blob: de2756e5ae67000f3bee0519a641ee980984ea5e [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;
53import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080054import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
55import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080056import org.onosproject.net.flow.criteria.MplsBosCriterion;
57import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070058import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chan30122a72017-10-05 15:17:15 -070059import org.onosproject.net.flow.criteria.TcpPortCriterion;
60import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070061import org.onosproject.net.flow.criteria.VlanIdCriterion;
62import org.onosproject.net.flow.instructions.Instruction;
63import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
64import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080065import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070066import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080067import org.onosproject.net.flow.instructions.L3ModificationInstruction;
68import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070069import org.onosproject.net.flowobjective.FilteringObjective;
70import org.onosproject.net.flowobjective.FlowObjectiveStore;
71import org.onosproject.net.flowobjective.ForwardingObjective;
72import org.onosproject.net.flowobjective.NextObjective;
73import org.onosproject.net.flowobjective.Objective;
74import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.group.DefaultGroupKey;
76import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070078import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070079import org.onosproject.store.serializers.KryoNamespaces;
80import org.slf4j.Logger;
81
Pier Ventree0ae7a32016-11-23 09:57:42 -080082import java.util.ArrayDeque;
83import java.util.ArrayList;
84import java.util.Collection;
85import java.util.Collections;
86import java.util.Deque;
87import java.util.List;
88import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080089import java.util.concurrent.ScheduledExecutorService;
90import java.util.concurrent.TimeUnit;
91
92import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080093import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chan206cac02017-10-20 19:09:16 -070094import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
95import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080096import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -080097import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070098import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chan30122a72017-10-05 15:17:15 -070099import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800100import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700101import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
102import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800103
Saurav Das822c4e22015-10-23 10:51:11 -0700104/**
105 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700106 */
Charles Chan361154b2016-03-24 10:23:39 -0700107public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700108 protected static final int PORT_TABLE = 0;
109 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800110 protected static final int VLAN_1_TABLE = 11;
111 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
112 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700113 protected static final int TMAC_TABLE = 20;
114 protected static final int UNICAST_ROUTING_TABLE = 30;
115 protected static final int MULTICAST_ROUTING_TABLE = 40;
116 protected static final int MPLS_TABLE_0 = 23;
117 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700118 protected static final int MPLS_L3_TYPE_TABLE = 27;
119 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700120 protected static final int BRIDGING_TABLE = 50;
121 protected static final int ACL_TABLE = 60;
122 protected static final int MAC_LEARNING_TABLE = 254;
123 protected static final long OFPP_MAX = 0xffffff00L;
124
Saurav Das52025962016-01-28 22:30:01 -0800125 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800126 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700127 protected static final int LOWEST_PRIORITY = 0x0;
128
Pier Ventre42287df2016-11-09 14:17:26 -0800129 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800130 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
131 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
132
Pier Ventre70d53ba2016-11-17 22:26:29 -0800133 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800134 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
135 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
136
Saurav Das822c4e22015-10-23 10:51:11 -0700137 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700138 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700139 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700140 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800141 protected GroupService groupService;
142 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700143 protected DeviceId deviceId;
144 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700145 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800146 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700147 .register(KryoNamespaces.API)
148 .register(GroupKey.class)
149 .register(DefaultGroupKey.class)
150 .register(OfdpaNextGroup.class)
151 .register(ArrayDeque.class)
152 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700153
Charles Chan425854b2016-04-11 15:32:12 -0700154 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700155
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700156 // flows installations to be retried
Charles Chanc6e64bb2018-03-02 13:26:22 -0800157 private ScheduledExecutorService executorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700158 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chanc6e64bb2018-03-02 13:26:22 -0800159 private static final int MAX_RETRY_ATTEMPTS = 10;
160 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800161
Saurav Das822c4e22015-10-23 10:51:11 -0700162 @Override
163 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700164 this.deviceId = deviceId;
165
Charles Chan425854b2016-04-11 15:32:12 -0700166 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700167 coreService = serviceDirectory.get(CoreService.class);
168 flowRuleService = serviceDirectory.get(FlowRuleService.class);
169 groupService = serviceDirectory.get(GroupService.class);
170 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700171 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700172
Charles Chan40132b32017-01-22 00:19:37 -0800173 initDriverId();
174 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700175
Saurav Das822c4e22015-10-23 10:51:11 -0700176 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700177 }
178
Charles Chan40132b32017-01-22 00:19:37 -0800179 protected void initDriverId() {
180 driverId = coreService.registerApplication(
181 "org.onosproject.driver.Ofdpa2Pipeline");
182 }
183
184 protected void initGroupHander(PipelinerContext context) {
185 groupHandler = new Ofdpa2GroupHandler();
186 groupHandler.init(deviceId, context);
187 }
188
Saurav Das822c4e22015-10-23 10:51:11 -0700189 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800190 // OF-DPA does not require initializing the pipeline as it puts default
191 // rules automatically in the hardware. However emulation of OFDPA in
192 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700193 }
194
Charles Chand1172632017-03-15 17:33:09 -0700195 /**
196 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
197 *
198 * @return true to use the extensions
199 */
200 protected boolean requireVlanExtensions() {
201 return true;
202 }
203
Saurav Das86d13e82017-04-28 17:03:48 -0700204 /**
205 * Determines whether in-port should be matched on in TMAC table rules.
206 *
207 * @return true if match on in-port should be programmed
208 */
209 protected boolean matchInPortTmacTable() {
210 return true;
211 }
212
Charles Chan30122a72017-10-05 15:17:15 -0700213 /**
214 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
215 *
216 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
217 */
218 protected boolean supportIpv6L4Dst() {
219 return true;
220 }
221
Saurav Das95047002018-01-25 09:49:01 -0800222 /**
223 * Determines whether this driver should continue to retry flows that point
224 * to empty groups. See CORD-554.
225 *
226 * @return true if the driver should retry flows
227 */
228 protected boolean shouldRetry() {
229 return true;
230 }
231
Saurav Das822c4e22015-10-23 10:51:11 -0700232 //////////////////////////////////////
233 // Flow Objectives
234 //////////////////////////////////////
235
236 @Override
237 public void filter(FilteringObjective filteringObjective) {
238 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
239 processFilter(filteringObjective,
240 filteringObjective.op() == Objective.Operation.ADD,
241 filteringObjective.appId());
242 } else {
243 // Note that packets that don't match the PERMIT filter are
244 // automatically denied. The DENY filter is used to deny packets
245 // that are otherwise permitted by the PERMIT filter.
246 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530247 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700248 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
249 }
250 }
251
252 @Override
253 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700254 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700255 if (rules == null || rules.isEmpty()) {
256 // Assumes fail message has already been generated to the objective
257 // context. Returning here prevents spurious pass message to be
258 // generated by FlowRule service for empty flowOps.
259 return;
260 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700261 sendForward(fwd, rules);
262 }
263
Charles Chanc6e64bb2018-03-02 13:26:22 -0800264 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700265 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700266 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700267 case ADD:
268 rules.stream()
269 .filter(Objects::nonNull)
270 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800271 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700272 break;
273 case REMOVE:
274 rules.stream()
275 .filter(Objects::nonNull)
276 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800277 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700278 break;
279 default:
280 fail(fwd, ObjectiveError.UNKNOWN);
281 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700282 }
283
Saurav Das822c4e22015-10-23 10:51:11 -0700284 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
285 @Override
286 public void onSuccess(FlowRuleOperations ops) {
287 pass(fwd);
288 }
289
290 @Override
291 public void onError(FlowRuleOperations ops) {
292 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
293 }
294 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700295 }
296
297 @Override
298 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800299 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
300 switch (nextObjective.op()) {
301 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800302 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800303 log.warn("Cannot add next {} that already exists in device {}",
304 nextObjective.id(), deviceId);
305 return;
306 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700307 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800308 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700309 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800310 break;
311 case ADD_TO_EXISTING:
312 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700313 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800314 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700315 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800316 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800317 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800318 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
319 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800320
321 // by design multiple pending bucket is allowed for the group
322 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
323 if (pendBkts == null) {
324 pendBkts = Sets.newHashSet();
325 }
326 pendBkts.add(nextObjective);
327 return pendBkts;
328 });
Saurav Das4f980082015-11-05 13:39:15 -0800329 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800330 break;
331 case REMOVE:
332 if (nextGroup == null) {
333 log.warn("Cannot remove next {} that does not exist in device {}",
334 nextObjective.id(), deviceId);
335 return;
336 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700337 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800338 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700339 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800340 break;
341 case REMOVE_FROM_EXISTING:
342 if (nextGroup == null) {
343 log.warn("Cannot remove from next {} that does not exist in device {}",
344 nextObjective.id(), deviceId);
345 return;
346 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700347 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800348 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700349 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800350 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700351 case VERIFY:
352 if (nextGroup == null) {
353 log.warn("Cannot verify next {} that does not exist in device {}",
354 nextObjective.id(), deviceId);
355 return;
356 }
357 log.debug("Processing NextObjective id {} in dev {} - verify",
358 nextObjective.id(), deviceId);
359 groupHandler.verifyGroup(nextObjective, nextGroup);
360 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800361 default:
Saurav Das4f980082015-11-05 13:39:15 -0800362 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700363 }
364 }
365
366 //////////////////////////////////////
367 // Flow handling
368 //////////////////////////////////////
369
370 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700371 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
372 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700373 *
374 * @param filt the filtering objective
375 * @param install indicates whether to add or remove the objective
376 * @param applicationId the application that sent this objective
377 */
Saurav Das52025962016-01-28 22:30:01 -0800378 protected void processFilter(FilteringObjective filt,
379 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700380 // This driver only processes filtering criteria defined with switch
381 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530382 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700383 EthCriterion ethCriterion = null;
384 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700385 if (!filt.key().equals(Criteria.dummy()) &&
386 filt.key().type() == Criterion.Type.IN_PORT) {
387 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700388 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530389 if (portCriterion == null) {
390 log.debug("No IN_PORT defined in filtering objective from app: {}",
391 applicationId);
392 } else {
393 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
394 portCriterion.port());
395 }
Saurav Das822c4e22015-10-23 10:51:11 -0700396 // convert filtering conditions for switch-intfs into flowrules
397 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
398 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700399 switch (criterion.type()) {
400 case ETH_DST:
401 case ETH_DST_MASKED:
402 ethCriterion = (EthCriterion) criterion;
403 break;
404 case VLAN_VID:
405 vidCriterion = (VlanIdCriterion) criterion;
406 break;
407 default:
408 log.warn("Unsupported filter {}", criterion);
409 fail(filt, ObjectiveError.UNSUPPORTED);
410 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700411 }
412 }
413
Saurav Das0e99e2b2015-10-28 12:39:42 -0700414 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800415 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700416 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chan0d6db512017-12-19 19:55:57 -0800417 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800418 assignedVlan = vidCriterion.vlanId();
Charles Chan0d6db512017-12-19 19:55:57 -0800419 } else if (filt.meta() != null) {
420 assignedVlan = readVlanFromTreatment(filt.meta());
Charles Chand55e84d2016-03-30 17:54:24 -0700421 }
Charles Chane849c192016-01-11 18:28:54 -0800422
Charles Chand55e84d2016-03-30 17:54:24 -0700423 if (assignedVlan == null) {
424 log.error("Driver fails to extract VLAN information. "
Frank Wang3a98e0a2017-08-11 11:09:30 +0800425 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700426 log.debug("VLAN ID in criterion={}, metadata={}",
427 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
428 fail(filt, ObjectiveError.BADPARAMS);
429 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700430 }
431 }
432
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800433 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700434 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700435 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700436 } else {
Charles Chan3aed59a2018-03-02 16:43:28 -0800437 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
438 vidCriterion, assignedVlan, applicationId);
439 for (List<FlowRule> flowRules : allStages) {
440 log.trace("Starting a new flow rule stage");
441 ops.newStage();
Charles Chan0d6db512017-12-19 19:55:57 -0800442
Charles Chan3aed59a2018-03-02 16:43:28 -0800443 for (FlowRule flowRule : flowRules) {
444 log.trace("{} flow rules in TMAC table: {} for dev: {}",
445 (install) ? "adding" : "removing", flowRules, deviceId);
446
447 if (install) {
448 ops = ops.add(flowRule);
Charles Chan0d6db512017-12-19 19:55:57 -0800449 } else {
Charles Chan3aed59a2018-03-02 16:43:28 -0800450 // 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(flowRule);
454 } else {
455 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
456 }
Charles Chan0d6db512017-12-19 19:55:57 -0800457 }
458 }
Saurav Das822c4e22015-10-23 10:51:11 -0700459 }
460 }
461
Charles Chan985b12e2016-05-11 19:47:22 -0700462 if (vidCriterion == null) {
463 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530464 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700465 } else {
Charles Chan3aed59a2018-03-02 16:43:28 -0800466 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800467 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan3aed59a2018-03-02 16:43:28 -0800468 for (List<FlowRule> flowRules : allStages) {
469 log.trace("Starting a new flow rule stage");
470 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800471
Charles Chan3aed59a2018-03-02 16:43:28 -0800472 for (FlowRule flowRule : flowRules) {
473 log.trace("{} flow rules in VLAN table: {} for dev: {}",
474 (install) ? "adding" : "removing", flowRule, deviceId);
475 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700476 }
Saurav Das822c4e22015-10-23 10:51:11 -0700477 }
478 }
479
Saurav Das822c4e22015-10-23 10:51:11 -0700480 // apply filtering flow rules
481 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
482 @Override
483 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800484 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700485 ops.stages().get(0).size(), deviceId);
486 pass(filt);
487 }
488
489 @Override
490 public void onError(FlowRuleOperations ops) {
491 log.info("Failed to apply all filtering rules in dev {}", deviceId);
492 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
493 }
494 }));
495
496 }
497
498 /**
Charles Chand1172632017-03-15 17:33:09 -0700499 * Internal implementation of processVlanIdFilter.
500 * <p>
501 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
502 * Since it is non-OF spec, we need an extension treatment for that.
503 * The useVlanExtension must be set to false for OFDPA i12.
504 * </p>
Charles Chan3aed59a2018-03-02 16:43:28 -0800505 * <p>
506 * NOTE: Separate VLAN filtering rules and assignment rules
507 * into different stages in order to guarantee that filtering rules
508 * always go first, as required by OFDPA.
509 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700510 *
Charles Chanf9e98652016-09-07 16:54:23 -0700511 * @param portCriterion port on device for which this filter is programmed
512 * @param vidCriterion vlan assigned to port, or NONE for untagged
513 * @param assignedVlan assigned vlan-id for untagged packets
514 * @param applicationId for application programming this filter
Charles Chan3aed59a2018-03-02 16:43:28 -0800515 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700516 */
Charles Chan3aed59a2018-03-02 16:43:28 -0800517 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700518 VlanIdCriterion vidCriterion,
519 VlanId assignedVlan,
520 ApplicationId applicationId) {
Charles Chan3aed59a2018-03-02 16:43:28 -0800521 List<FlowRule> filteringRules = new ArrayList<>();
522 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700523 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
524 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800525 TrafficSelector.Builder preSelector = null;
526 TrafficTreatment.Builder preTreatment = null;
527
Saurav Das4f980082015-11-05 13:39:15 -0800528 treatment.transition(TMAC_TABLE);
529
Saurav Das822c4e22015-10-23 10:51:11 -0700530 if (vidCriterion.vlanId() == VlanId.NONE) {
531 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700532 preSelector = DefaultTrafficSelector.builder();
533 if (requireVlanExtensions()) {
534 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
535 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700536 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
537 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700538
539 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
540 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700541 } else {
Charles Chand1172632017-03-15 17:33:09 -0700542 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700543 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700544
545 preSelector.matchVlanId(assignedVlan);
546 }
547 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
548 } else {
549 if (requireVlanExtensions()) {
550 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
551 selector.extension(ofdpaMatchVlanVid, deviceId);
552 } else {
553 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700554 }
Charles Chan14967c22015-12-07 11:11:50 -0800555
Charles Chand55e84d2016-03-30 17:54:24 -0700556 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700557 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800558 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
559 treatment.extension(ofdpaSetVlanVid, deviceId);
560 } else {
561 treatment.setVlanId(assignedVlan);
562 }
Charles Chand55e84d2016-03-30 17:54:24 -0700563 }
Saurav Das822c4e22015-10-23 10:51:11 -0700564 }
Saurav Das822c4e22015-10-23 10:51:11 -0700565
566 // ofdpa cannot match on ALL portnumber, so we need to use separate
567 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800568 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530569 if (portCriterion != null) {
570 if (PortNumber.ALL.equals(portCriterion.port())) {
571 for (Port port : deviceService.getPorts(deviceId)) {
572 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
573 portnums.add(port.number());
574 }
Saurav Das822c4e22015-10-23 10:51:11 -0700575 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530576 } else {
577 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700578 }
579 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530580 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan3aed59a2018-03-02 16:43:28 -0800581 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700582 }
Saurav Das4f980082015-11-05 13:39:15 -0800583
Saurav Das822c4e22015-10-23 10:51:11 -0700584 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800585 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700586 selector.matchInPort(pnum);
587 FlowRule rule = DefaultFlowRule.builder()
588 .forDevice(deviceId)
589 .withSelector(selector.build())
590 .withTreatment(treatment.build())
591 .withPriority(DEFAULT_PRIORITY)
592 .fromApp(applicationId)
593 .makePermanent()
594 .forTable(VLAN_TABLE).build();
Charles Chan3aed59a2018-03-02 16:43:28 -0800595 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800596
597 if (preSelector != null) {
598 preSelector.matchInPort(pnum);
599 FlowRule preRule = DefaultFlowRule.builder()
600 .forDevice(deviceId)
601 .withSelector(preSelector.build())
602 .withTreatment(preTreatment.build())
603 .withPriority(DEFAULT_PRIORITY)
604 .fromApp(applicationId)
605 .makePermanent()
606 .forTable(VLAN_TABLE).build();
Charles Chan3aed59a2018-03-02 16:43:28 -0800607 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800608 }
Saurav Das822c4e22015-10-23 10:51:11 -0700609 }
Charles Chan3aed59a2018-03-02 16:43:28 -0800610 return ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700611 }
612
613 /**
614 * Allows routed packets with correct destination MAC to be directed
615 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700616 *
617 * @param portCriterion port on device for which this filter is programmed
618 * @param ethCriterion dstMac of device for which is filter is programmed
619 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700620 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700621 * @param applicationId for application programming this filter
Charles Chan3aed59a2018-03-02 16:43:28 -0800622 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700623
624 */
Charles Chan3aed59a2018-03-02 16:43:28 -0800625 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700626 EthCriterion ethCriterion,
627 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700628 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700629 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800630 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530631 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800632 return processEthDstOnlyFilter(ethCriterion, applicationId);
633 }
634
Charles Chan5b9df8d2016-03-28 22:21:40 -0700635 // Multicast MAC
636 if (ethCriterion.mask() != null) {
Charles Chan206cac02017-10-20 19:09:16 -0700637 return processMcastEthDstFilter(ethCriterion, assignedVlan, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700638 }
639
Saurav Das822c4e22015-10-23 10:51:11 -0700640 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530641 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700642 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700643 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530644 List<FlowRule> rules = new ArrayList<>();
645 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
646 if (vidCriterion != null && requireVlanExtensions()) {
647 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
648 }
Saurav Das822c4e22015-10-23 10:51:11 -0700649 // ofdpa cannot match on ALL portnumber, so we need to use separate
650 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700651 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530652 if (portCriterion != null) {
653 if (PortNumber.ALL.equals(portCriterion.port())) {
654 for (Port port : deviceService.getPorts(deviceId)) {
655 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
656 portnums.add(port.number());
657 }
Saurav Das822c4e22015-10-23 10:51:11 -0700658 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530659 } else {
660 portnums.add(portCriterion.port());
661 }
662 for (PortNumber pnum : portnums) {
663 rules.add(buildTmacRuleForIpv4(ethCriterion,
664 vidCriterion,
665 ofdpaMatchVlanVid,
666 applicationId,
667 pnum));
668 rules.add(buildTmacRuleForMpls(ethCriterion,
669 vidCriterion,
670 ofdpaMatchVlanVid,
671 applicationId,
672 pnum));
673 rules.add(buildTmacRuleForIpv6(ethCriterion,
674 vidCriterion,
675 ofdpaMatchVlanVid,
676 applicationId,
677 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700678 }
679 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530680 rules.add(buildTmacRuleForIpv4(ethCriterion,
681 vidCriterion,
682 ofdpaMatchVlanVid,
683 applicationId,
684 null));
685 rules.add(buildTmacRuleForMpls(ethCriterion,
686 vidCriterion,
687 ofdpaMatchVlanVid,
688 applicationId,
689 null));
690 rules.add(buildTmacRuleForIpv6(ethCriterion,
691 vidCriterion,
692 ofdpaMatchVlanVid,
693 applicationId,
694 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700695 }
Charles Chan3aed59a2018-03-02 16:43:28 -0800696 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700697 }
698
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530699 /**
700 * Builds TMAC rules for IPv4 packets.
701 *
Charles Chan0d6db512017-12-19 19:55:57 -0800702 * @param ethCriterion dst mac matching
703 * @param vidCriterion vlan id assigned to the port
704 * @param ofdpaMatchVlanVid OFDPA vlan id matching
705 * @param applicationId application id
706 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530707 * @return TMAC rule for IPV4 packets
708 */
709 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
710 VlanIdCriterion vidCriterion,
711 OfdpaMatchVlanVid ofdpaMatchVlanVid,
712 ApplicationId applicationId,
713 PortNumber pnum) {
714 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
715 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
716 if (pnum != null) {
717 if (matchInPortTmacTable()) {
718 selector.matchInPort(pnum);
719 } else {
Saurav Das9aa28bd2017-12-05 15:00:23 -0800720 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530721 "ignoring the IN_PORT criteria");
722 }
723 }
724 if (vidCriterion != null) {
725 if (requireVlanExtensions()) {
726 selector.extension(ofdpaMatchVlanVid, deviceId);
727 } else {
728 selector.matchVlanId(vidCriterion.vlanId());
729 }
730 }
731 selector.matchEthType(Ethernet.TYPE_IPV4);
732 selector.matchEthDst(ethCriterion.mac());
733 treatment.transition(UNICAST_ROUTING_TABLE);
734 return DefaultFlowRule.builder()
735 .forDevice(deviceId)
736 .withSelector(selector.build())
737 .withTreatment(treatment.build())
738 .withPriority(DEFAULT_PRIORITY)
739 .fromApp(applicationId)
740 .makePermanent()
741 .forTable(TMAC_TABLE).build();
742 }
743
744 /**
745 * Builds TMAC rule for MPLS packets.
746 *
Charles Chan0d6db512017-12-19 19:55:57 -0800747 * @param ethCriterion dst mac matching
748 * @param vidCriterion vlan id assigned to the port
749 * @param ofdpaMatchVlanVid OFDPA vlan id matching
750 * @param applicationId application id
751 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530752 * @return TMAC rule for MPLS packets
753 */
754 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
755 VlanIdCriterion vidCriterion,
756 OfdpaMatchVlanVid ofdpaMatchVlanVid,
757 ApplicationId applicationId,
758 PortNumber pnum) {
759 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
760 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
761 if (pnum != null) {
762 if (matchInPortTmacTable()) {
763 selector.matchInPort(pnum);
764 } else {
Saurav Das9aa28bd2017-12-05 15:00:23 -0800765 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530766 "ignoring the IN_PORT criteria");
767 }
768 }
769 if (vidCriterion != null) {
770 if (requireVlanExtensions()) {
771 selector.extension(ofdpaMatchVlanVid, deviceId);
772 } else {
773 selector.matchVlanId(vidCriterion.vlanId());
774 }
775 }
776 selector.matchEthType(Ethernet.MPLS_UNICAST);
777 selector.matchEthDst(ethCriterion.mac());
778 treatment.transition(MPLS_TABLE_0);
779 return DefaultFlowRule.builder()
780 .forDevice(deviceId)
781 .withSelector(selector.build())
782 .withTreatment(treatment.build())
783 .withPriority(DEFAULT_PRIORITY)
784 .fromApp(applicationId)
785 .makePermanent()
786 .forTable(TMAC_TABLE).build();
787 }
788
789 /**
790 * Builds TMAC rules for IPv6 packets.
791 *
Charles Chan0d6db512017-12-19 19:55:57 -0800792 * @param ethCriterion dst mac matching
793 * @param vidCriterion vlan id assigned to the port
794 * @param ofdpaMatchVlanVid OFDPA vlan id matching
795 * @param applicationId application id
796 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530797 * @return TMAC rule for IPV6 packets
798 */
799 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
800 VlanIdCriterion vidCriterion,
801 OfdpaMatchVlanVid ofdpaMatchVlanVid,
802 ApplicationId applicationId,
803 PortNumber pnum) {
804 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
805 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
806 if (pnum != null) {
807 if (matchInPortTmacTable()) {
808 selector.matchInPort(pnum);
809 } else {
Saurav Das9aa28bd2017-12-05 15:00:23 -0800810 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530811 "ignoring the IN_PORT criteria");
812 }
813 }
814 if (vidCriterion != null) {
815 if (requireVlanExtensions()) {
816 selector.extension(ofdpaMatchVlanVid, deviceId);
817 } else {
818 selector.matchVlanId(vidCriterion.vlanId());
819 }
820 }
821 selector.matchEthType(Ethernet.TYPE_IPV6);
822 selector.matchEthDst(ethCriterion.mac());
823 treatment.transition(UNICAST_ROUTING_TABLE);
824 return DefaultFlowRule.builder()
825 .forDevice(deviceId)
826 .withSelector(selector.build())
827 .withTreatment(treatment.build())
828 .withPriority(DEFAULT_PRIORITY)
829 .fromApp(applicationId)
830 .makePermanent()
831 .forTable(TMAC_TABLE).build();
832 }
833
Charles Chan3aed59a2018-03-02 16:43:28 -0800834 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700835 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800836 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
837
Charles Chan5270ed02016-01-30 23:22:37 -0800838 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
839 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
840 selector.matchEthType(Ethernet.TYPE_IPV4);
841 selector.matchEthDst(ethCriterion.mac());
842 treatment.transition(UNICAST_ROUTING_TABLE);
843 FlowRule rule = DefaultFlowRule.builder()
844 .forDevice(deviceId)
845 .withSelector(selector.build())
846 .withTreatment(treatment.build())
847 .withPriority(DEFAULT_PRIORITY)
848 .fromApp(applicationId)
849 .makePermanent()
850 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800851 builder.add(rule);
852
853 selector = DefaultTrafficSelector.builder();
854 treatment = DefaultTrafficTreatment.builder();
855 selector.matchEthType(Ethernet.TYPE_IPV6);
856 selector.matchEthDst(ethCriterion.mac());
857 treatment.transition(UNICAST_ROUTING_TABLE);
858 rule = DefaultFlowRule.builder()
859 .forDevice(deviceId)
860 .withSelector(selector.build())
861 .withTreatment(treatment.build())
862 .withPriority(DEFAULT_PRIORITY)
863 .fromApp(applicationId)
864 .makePermanent()
865 .forTable(TMAC_TABLE).build();
Charles Chan3aed59a2018-03-02 16:43:28 -0800866 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800867 }
868
Charles Chan3aed59a2018-03-02 16:43:28 -0800869 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chan206cac02017-10-20 19:09:16 -0700870 VlanId assignedVlan,
Yi Tsengef19de12017-04-24 11:33:05 -0700871 ApplicationId applicationId) {
Julia Fergusond8f145e2017-08-10 18:15:24 +0000872 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700873 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
874 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan206cac02017-10-20 19:09:16 -0700875 FlowRule rule;
Julia Fergusond8f145e2017-08-10 18:15:24 +0000876
Charles Chan206cac02017-10-20 19:09:16 -0700877 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
878 selector.matchEthType(Ethernet.TYPE_IPV4);
879 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
880 selector.matchVlanId(assignedVlan);
881 treatment.transition(MULTICAST_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 builder.add(rule);
891 }
892
893 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
894 selector = DefaultTrafficSelector.builder();
895 treatment = DefaultTrafficTreatment.builder();
896 selector.matchEthType(Ethernet.TYPE_IPV6);
897 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
898 selector.matchVlanId(assignedVlan);
899 treatment.transition(MULTICAST_ROUTING_TABLE);
900 rule = DefaultFlowRule.builder()
901 .forDevice(deviceId)
902 .withSelector(selector.build())
903 .withTreatment(treatment.build())
904 .withPriority(DEFAULT_PRIORITY)
905 .fromApp(applicationId)
906 .makePermanent()
907 .forTable(TMAC_TABLE).build();
908 builder.add(rule);
909 }
Charles Chan3aed59a2018-03-02 16:43:28 -0800910 return ImmutableList.of(builder.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700911 }
912
Saurav Das822c4e22015-10-23 10:51:11 -0700913 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
914 switch (fwd.flag()) {
915 case SPECIFIC:
916 return processSpecific(fwd);
917 case VERSATILE:
918 return processVersatile(fwd);
919 default:
920 fail(fwd, ObjectiveError.UNKNOWN);
921 log.warn("Unknown forwarding flag {}", fwd.flag());
922 }
923 return Collections.emptySet();
924 }
925
926 /**
927 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
928 * ACL table.
929 * @param fwd the forwarding objective of type 'versatile'
930 * @return a collection of flow rules to be sent to the switch. An empty
931 * collection may be returned if there is a problem in processing
932 * the flow rule
933 */
Saurav Das52025962016-01-28 22:30:01 -0800934 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800935 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800936 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700937
938 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800939 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700940 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800941 log.error("Versatile forwarding objective:{} must include ethType",
942 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700943 fail(fwd, ObjectiveError.BADPARAMS);
944 return Collections.emptySet();
945 }
946 if (fwd.nextId() == null && fwd.treatment() == null) {
947 log.error("Forwarding objective {} from {} must contain "
948 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800949 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700950 return Collections.emptySet();
951 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800952
Saurav Das77b5e902016-01-27 17:01:59 -0800953 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
954 fwd.selector().criteria().forEach(criterion -> {
955 if (criterion instanceof VlanIdCriterion) {
956 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
957 // ensure that match does not include vlan = NONE as OF-DPA does not
958 // match untagged packets this way in the ACL table.
959 if (vlanId.equals(VlanId.NONE)) {
960 return;
961 }
Charles Chand1172632017-03-15 17:33:09 -0700962 if (requireVlanExtensions()) {
963 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
964 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
965 } else {
966 sbuilder.matchVlanId(vlanId);
967 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800968 } else if (criterion instanceof Icmpv6TypeCriterion ||
969 criterion instanceof Icmpv6CodeCriterion) {
970 /*
971 * We silenty discard these criterions, our current
972 * OFDPA platform does not support these matches on
973 * the ACL table.
974 */
975 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Charles Chan30122a72017-10-05 15:17:15 -0700976 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
977 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
978 // Currently L4 dst port matching is only used by DHCP relay feature
979 // and therefore is safe to be replaced with L4 src port matching.
980 // We need to revisit this if L4 dst port is used for other purpose in the future.
981 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chan30122a72017-10-05 15:17:15 -0700982 switch (criterion.type()) {
983 case UDP_DST:
Charles Chan30122a72017-10-05 15:17:15 -0700984 case UDP_DST_MASKED:
Charles Chan30122a72017-10-05 15:17:15 -0700985 case TCP_DST:
Charles Chan30122a72017-10-05 15:17:15 -0700986 case TCP_DST_MASKED:
Charles Chan30122a72017-10-05 15:17:15 -0700987 break;
988 default:
989 sbuilder.add(criterion);
990 }
991 } else {
992 sbuilder.add(criterion);
993 }
Saurav Das77b5e902016-01-27 17:01:59 -0800994 } else {
995 sbuilder.add(criterion);
996 }
997 });
998
Saurav Das822c4e22015-10-23 10:51:11 -0700999 // XXX driver does not currently do type checking as per Tables 65-67 in
1000 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001001 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1002 if (fwd.treatment() != null) {
1003 for (Instruction ins : fwd.treatment().allInstructions()) {
1004 if (ins instanceof OutputInstruction) {
1005 OutputInstruction o = (OutputInstruction) ins;
Charles Chanc6e64bb2018-03-02 13:26:22 -08001006 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001007 ttBuilder.add(o);
1008 } else {
1009 log.warn("Only allowed treatments in versatile forwarding "
1010 + "objectives are punts to the controller");
1011 }
1012 } else {
1013 log.warn("Cannot process instruction in versatile fwd {}", ins);
1014 }
Saurav Das822c4e22015-10-23 10:51:11 -07001015 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001016 if (fwd.treatment().clearedDeferred()) {
1017 ttBuilder.wipeDeferred();
1018 }
Saurav Das822c4e22015-10-23 10:51:11 -07001019 }
Saurav Das822c4e22015-10-23 10:51:11 -07001020 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001021 // overide case
1022 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301023 if (next == null) {
1024 fail(fwd, ObjectiveError.BADPARAMS);
1025 return Collections.emptySet();
1026 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001027 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1028 // we only need the top level group's key to point the flow to it
1029 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1030 if (group == null) {
1031 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1032 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1033 fail(fwd, ObjectiveError.GROUPMISSING);
1034 return Collections.emptySet();
1035 }
1036 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001037 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001038
1039 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1040 .fromApp(fwd.appId())
1041 .withPriority(fwd.priority())
1042 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001043 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001044 .withTreatment(ttBuilder.build())
1045 .makePermanent()
1046 .forTable(ACL_TABLE);
1047 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001048 }
1049
1050 /**
1051 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001052 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001053 *
1054 * @param fwd the forwarding objective of type 'specific'
1055 * @return a collection of flow rules. Typically there will be only one
1056 * for this type of forwarding objective. An empty set may be
1057 * returned if there is an issue in processing the objective.
1058 */
Charles Chanc6e64bb2018-03-02 13:26:22 -08001059 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001060 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001061 fwd.id(), deviceId, fwd.nextId());
1062 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1063 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1064
1065 if (isEthTypeObj) {
1066 return processEthTypeSpecific(fwd);
1067 } else if (isEthDstObj) {
1068 return processEthDstSpecific(fwd);
1069 } else {
1070 log.warn("processSpecific: Unsupported forwarding objective "
1071 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001072 fail(fwd, ObjectiveError.UNSUPPORTED);
1073 return Collections.emptySet();
1074 }
Saurav Das4ce45962015-11-24 23:21:05 -08001075 }
1076
Saurav Das4ce45962015-11-24 23:21:05 -08001077 /**
1078 * Handles forwarding rules to the IP and MPLS tables.
1079 *
1080 * @param fwd the forwarding objective
1081 * @return A collection of flow rules, or an empty set
1082 */
1083 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001084 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001085 }
1086
1087 /**
1088 * Internal implementation of processEthTypeSpecific.
1089 * <p>
1090 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1091 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1092 * The allowDefaultRoute must be set to false for OFDPA i12.
1093 * </p>
1094 *
1095 * @param fwd the forwarding objective
1096 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001097 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001098 * @return A collection of flow rules, or an empty set
1099 */
1100 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001101 boolean allowDefaultRoute,
1102 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001103 TrafficSelector selector = fwd.selector();
1104 EthTypeCriterion ethType =
1105 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -08001106 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001107 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001108 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001109 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001110 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001111 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001112
Saurav Das8a0732e2015-11-20 15:27:53 -08001113 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001114 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1115 return Collections.emptyList();
1116 }
1117 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001118 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001119 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001120 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001121 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001122 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001123 }
Charles Chanc6e64bb2018-03-02 13:26:22 -08001124
1125 /*
1126 // XXX decrementing IP ttl is done automatically for routing, this
1127 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan14967c22015-12-07 11:11:50 -08001128 if (fwd.treatment() != null) {
1129 for (Instruction instr : fwd.treatment().allInstructions()) {
1130 if (instr instanceof L3ModificationInstruction &&
1131 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Charles Chanc6e64bb2018-03-02 13:26:22 -08001132 tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001133 }
1134 }
1135 }
Charles Chanc6e64bb2018-03-02 13:26:22 -08001136 */
Pier Ventree0ae7a32016-11-23 09:57:42 -08001137 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1138 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1139 return Collections.emptyList();
1140 }
Kalhee Kim7c6f4f82017-10-04 14:59:11 +00001141 //We need to set the proper next table
1142 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1143 if (ipv6Dst.isMulticast()) {
1144 forTableId = MULTICAST_ROUTING_TABLE;
1145 } else {
1146 forTableId = UNICAST_ROUTING_TABLE;
1147 }
1148
Charles Chanc6e64bb2018-03-02 13:26:22 -08001149 // XXX decrementing IP ttl is done automatically for routing, this
1150 // action is ignored or rejected in ofdpa as it is not fully implemented
1151 /*
Pier Ventree0ae7a32016-11-23 09:57:42 -08001152 if (fwd.treatment() != null) {
1153 for (Instruction instr : fwd.treatment().allInstructions()) {
1154 if (instr instanceof L3ModificationInstruction &&
1155 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Charles Chanc6e64bb2018-03-02 13:26:22 -08001156 tb.deferred().add(instr);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001157 }
1158 }
1159 }
Charles Chanc6e64bb2018-03-02 13:26:22 -08001160 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001161 } else {
1162 filteredSelector
1163 .matchEthType(Ethernet.MPLS_UNICAST)
1164 .matchMplsLabel(((MplsCriterion)
1165 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1166 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001167 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -08001168 if (bos != null) {
1169 filteredSelector.matchMplsBos(bos.mplsBos());
1170 }
1171 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001172 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1173 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001174
Charles Chan14967c22015-12-07 11:11:50 -08001175 if (fwd.treatment() != null) {
1176 for (Instruction instr : fwd.treatment().allInstructions()) {
1177 if (instr instanceof L2ModificationInstruction &&
1178 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001179 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001180 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001181 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001182 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1183 tb.immediate().popMpls();
1184 }
Charles Chan14967c22015-12-07 11:11:50 -08001185 }
1186 if (instr instanceof L3ModificationInstruction &&
1187 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1188 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1189 tb.immediate().decMplsTtl();
1190 }
1191 if (instr instanceof L3ModificationInstruction &&
1192 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1193 tb.immediate().add(instr);
1194 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001195 }
1196 }
1197 }
Saurav Das822c4e22015-10-23 10:51:11 -07001198
1199 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001200 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1201 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001202 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1203 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001204 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1205 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1206 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001207 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001208 return Collections.emptySet();
1209 }
1210
Saurav Das423fe2b2015-12-04 10:52:59 -08001211 NextGroup next = getGroupForNextObjective(fwd.nextId());
1212 if (next != null) {
1213 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1214 // we only need the top level group's key to point the flow to it
1215 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001216 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1217 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1218 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1219 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1220 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1221 return Collections.emptySet();
1222 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001223 if (group == null) {
1224 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1225 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1226 fail(fwd, ObjectiveError.GROUPMISSING);
1227 return Collections.emptySet();
1228 }
1229 tb.deferred().group(group.id());
Saurav Das95047002018-01-25 09:49:01 -08001230 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001231 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Das95047002018-01-25 09:49:01 -08001232 if (shouldRetry()) {
1233 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1234 Integer.toHexString(group.id().id()), deviceId,
1235 fwd.id());
1236 emptyGroup = true;
1237 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001238 }
Saurav Das25190812016-05-27 13:54:07 -07001239 } else {
1240 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1241 fwd.nextId(), deviceId, fwd.id());
1242 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1243 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001244 }
Saurav Das822c4e22015-10-23 10:51:11 -07001245 }
Charles Chancad338a2016-09-16 18:03:11 -07001246
1247 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001248 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001249 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001250 // set mpls type as apply_action
1251 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001252 }
1253 tb.transition(mplsNextTable);
1254 } else {
1255 tb.transition(ACL_TABLE);
1256 }
1257
Saurav Das822c4e22015-10-23 10:51:11 -07001258 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1259 .fromApp(fwd.appId())
1260 .withPriority(fwd.priority())
1261 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001262 .withSelector(filteredSelector.build())
1263 .withTreatment(tb.build())
1264 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001265
1266 if (fwd.permanent()) {
1267 ruleBuilder.makePermanent();
1268 } else {
1269 ruleBuilder.makeTemporary(fwd.timeout());
1270 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001271 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1272 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001273 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001274 flowRuleCollection.add(
1275 defaultRoute(fwd, complementarySelector, forTableId, tb)
1276 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001277 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1278 }
Saurav Das95047002018-01-25 09:49:01 -08001279
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001280 if (emptyGroup) {
1281 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1282 RETRY_MS, TimeUnit.MILLISECONDS);
1283 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001284 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001285 }
1286
Charles Chanc6e64bb2018-03-02 13:26:22 -08001287 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001288 TrafficSelector.Builder extBuilder,
1289 ForwardingObjective fwd,
1290 boolean allowDefaultRoute) {
1291 TrafficSelector selector = fwd.selector();
1292
1293 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1294 if (ipv4Dst.isMulticast()) {
1295 if (ipv4Dst.prefixLength() != 32) {
1296 log.warn("Multicast specific forwarding objective can only be /32");
1297 fail(fwd, ObjectiveError.BADPARAMS);
1298 return -1;
1299 }
1300 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1301 if (assignedVlan == null) {
1302 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1303 fail(fwd, ObjectiveError.BADPARAMS);
1304 return -1;
1305 }
Charles Chand1172632017-03-15 17:33:09 -07001306 if (requireVlanExtensions()) {
1307 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1308 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1309 } else {
1310 builderToUpdate.matchVlanId(assignedVlan);
1311 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001312 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1313 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1314 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1315 } else {
1316 if (ipv4Dst.prefixLength() == 0) {
1317 if (allowDefaultRoute) {
1318 // The entire IPV4_DST field is wildcarded intentionally
1319 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1320 } else {
1321 // NOTE: The switch does not support matching 0.0.0.0/0
1322 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1323 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1324 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1325 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1326 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1327 }
1328 } else {
1329 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1330 }
1331 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1332 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1333 }
1334 return 0;
1335 }
1336
1337 /**
1338 * Helper method to build Ipv6 selector using the selector provided by
1339 * a forwarding objective.
1340 *
1341 * @param builderToUpdate the builder to update
1342 * @param fwd the selector to read
1343 * @return 0 if the update ends correctly. -1 if the matches
1344 * are not yet supported
1345 */
Charles Chanc6e64bb2018-03-02 13:26:22 -08001346 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001347 ForwardingObjective fwd) {
1348
1349 TrafficSelector selector = fwd.selector();
1350
1351 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1352 if (ipv6Dst.isMulticast()) {
Julia Fergusond8f145e2017-08-10 18:15:24 +00001353 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1354 log.warn("Multicast specific forwarding objective can only be /128");
1355 fail(fwd, ObjectiveError.BADPARAMS);
1356 return -1;
1357 }
1358 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1359 if (assignedVlan == null) {
1360 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1361 fail(fwd, ObjectiveError.BADPARAMS);
1362 return -1;
1363 }
1364 if (requireVlanExtensions()) {
1365 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1366 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1367 } else {
1368 builderToUpdate.matchVlanId(assignedVlan);
1369 }
1370 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1371 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1372 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1373 } else {
1374 if (ipv6Dst.prefixLength() != 0) {
1375 builderToUpdate.matchIPv6Dst(ipv6Dst);
1376 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001377 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1378 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1379 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Fergusond8f145e2017-08-10 18:15:24 +00001380 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001381 return 0;
1382 }
1383
Charles Chanc6e64bb2018-03-02 13:26:22 -08001384 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001385 TrafficSelector.Builder complementarySelector,
1386 int forTableId,
1387 TrafficTreatment.Builder tb) {
1388 FlowRule.Builder rule = DefaultFlowRule.builder()
1389 .fromApp(fwd.appId())
1390 .withPriority(fwd.priority())
1391 .forDevice(deviceId)
1392 .withSelector(complementarySelector.build())
1393 .withTreatment(tb.build())
1394 .forTable(forTableId);
1395 if (fwd.permanent()) {
1396 rule.makePermanent();
1397 } else {
1398 rule.makeTemporary(fwd.timeout());
1399 }
1400 return rule.build();
1401 }
1402
Saurav Das4ce45962015-11-24 23:21:05 -08001403 /**
1404 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1405 * allowed in the bridging table - instead we use L2 Interface group or
1406 * L2 flood group
1407 *
1408 * @param fwd the forwarding objective
1409 * @return A collection of flow rules, or an empty set
1410 */
1411 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1412 List<FlowRule> rules = new ArrayList<>();
1413
1414 // Build filtered selector
1415 TrafficSelector selector = fwd.selector();
1416 EthCriterion ethCriterion = (EthCriterion) selector
1417 .getCriterion(Criterion.Type.ETH_DST);
1418 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1419 .getCriterion(Criterion.Type.VLAN_VID);
1420
1421 if (vlanIdCriterion == null) {
1422 log.warn("Forwarding objective for bridging requires vlan. Not "
1423 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1424 fail(fwd, ObjectiveError.BADPARAMS);
1425 return Collections.emptySet();
1426 }
1427
1428 TrafficSelector.Builder filteredSelectorBuilder =
1429 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001430
1431 if (!ethCriterion.mac().equals(NONE) &&
1432 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001433 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1434 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1435 fwd.id(), fwd.nextId(), deviceId);
1436 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001437 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001438 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1439 + "in dev:{} for vlan:{}",
1440 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1441 }
Charles Chand1172632017-03-15 17:33:09 -07001442 if (requireVlanExtensions()) {
1443 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1444 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1445 } else {
1446 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1447 }
Saurav Das4ce45962015-11-24 23:21:05 -08001448 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1449
1450 if (fwd.treatment() != null) {
1451 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1452 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1453 }
1454
1455 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1456 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001457 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001458 if (next != null) {
1459 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1460 // we only need the top level group's key to point the flow to it
1461 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1462 if (group != null) {
1463 treatmentBuilder.deferred().group(group.id());
1464 } else {
1465 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1466 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1467 fail(fwd, ObjectiveError.GROUPMISSING);
1468 return Collections.emptySet();
1469 }
1470 }
1471 }
1472 treatmentBuilder.immediate().transition(ACL_TABLE);
1473 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1474
1475 // Build bridging table entries
1476 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1477 flowRuleBuilder.fromApp(fwd.appId())
1478 .withPriority(fwd.priority())
1479 .forDevice(deviceId)
1480 .withSelector(filteredSelector)
1481 .withTreatment(filteredTreatment)
1482 .forTable(BRIDGING_TABLE);
1483 if (fwd.permanent()) {
1484 flowRuleBuilder.makePermanent();
1485 } else {
1486 flowRuleBuilder.makeTemporary(fwd.timeout());
1487 }
1488 rules.add(flowRuleBuilder.build());
1489 return rules;
1490 }
1491
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001492 //////////////////////////////////////
1493 // Helper Methods and Classes
1494 //////////////////////////////////////
1495
1496 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1497 TrafficSelector selector = fwd.selector();
1498 EthTypeCriterion ethType = (EthTypeCriterion) selector
1499 .getCriterion(Criterion.Type.ETH_TYPE);
1500 return !((ethType == null) ||
1501 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001502 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1503 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001504 }
1505
1506 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1507 TrafficSelector selector = fwd.selector();
1508 EthCriterion ethDst = (EthCriterion) selector
1509 .getCriterion(Criterion.Type.ETH_DST);
1510 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1511 .getCriterion(Criterion.Type.VLAN_VID);
1512 return !(ethDst == null && vlanId == null);
1513 }
1514
Charles Chanc6e64bb2018-03-02 13:26:22 -08001515 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001516 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1517 if (next != null) {
1518 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1519 if (gkeys != null && !gkeys.isEmpty()) {
1520 return next;
1521 } else {
1522 log.warn("Empty next group found in FlowObjective store for "
1523 + "next-id:{} in dev:{}", nextId, deviceId);
1524 }
1525 } else {
1526 log.warn("next-id {} not found in Flow objective store for dev:{}",
1527 nextId, deviceId);
1528 }
1529 return null;
1530 }
1531
Charles Chan188ebf52015-12-23 00:15:11 -08001532 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001533 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001534 }
1535
Charles Chan188ebf52015-12-23 00:15:11 -08001536 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001537 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001538 }
Saurav Das24431192016-03-07 19:13:00 -08001539
Saurav Das24431192016-03-07 19:13:00 -08001540 @Override
1541 public List<String> getNextMappings(NextGroup nextGroup) {
1542 List<String> mappings = new ArrayList<>();
1543 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1544 for (Deque<GroupKey> gkd : gkeys) {
1545 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001546 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001547 for (GroupKey gk : gkd) {
1548 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001549 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001550 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001551 continue;
1552 }
1553 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1554 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001555 lastGroup = g;
1556 }
1557 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001558 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001559 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001560 lastGroupIns = lastGroup.buckets().buckets().get(0)
1561 .treatment().allInstructions();
1562 }
1563 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001564 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001565 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001566 }
1567 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001568 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001569 }
1570 return mappings;
1571 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001572
Pier Ventre140a8942016-11-02 07:26:38 -07001573 static boolean isMplsBos(TrafficSelector selector) {
1574 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1575 return bosCriterion != null && bosCriterion.mplsBos();
1576 }
1577
1578 static boolean isNotMplsBos(TrafficSelector selector) {
1579 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1580 return bosCriterion != null && !bosCriterion.mplsBos();
1581 }
1582
Charles Chan30122a72017-10-05 15:17:15 -07001583 private static boolean isIpv6(TrafficSelector selector) {
1584 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1585 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1586 }
1587
1588 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001589 if (selector == null) {
1590 return null;
1591 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001592 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1593 return (criterion == null)
1594 ? null : ((VlanIdCriterion) criterion).vlanId();
1595 }
1596
Charles Chan30122a72017-10-05 15:17:15 -07001597 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001598 if (selector == null) {
1599 return null;
1600 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001601 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1602 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1603 }
Charles Chand55e84d2016-03-30 17:54:24 -07001604
1605 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001606 if (treatment == null) {
1607 return null;
1608 }
Charles Chand55e84d2016-03-30 17:54:24 -07001609 for (Instruction i : treatment.allInstructions()) {
1610 if (i instanceof ModVlanIdInstruction) {
1611 return ((ModVlanIdInstruction) i).vlanId();
1612 }
1613 }
1614 return null;
1615 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001616
1617 /**
1618 * Utility class that retries sending flows a fixed number of times, even if
1619 * some of the attempts are successful. Used only for forwarding objectives.
1620 */
1621 protected final class RetryFlows implements Runnable {
1622 int attempts = MAX_RETRY_ATTEMPTS;
1623 private Collection<FlowRule> retryFlows;
1624 private ForwardingObjective fwd;
1625
1626 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1627 this.fwd = fwd;
1628 this.retryFlows = retryFlows;
1629 }
1630
1631 @Override
1632 public void run() {
1633 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1634 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1635 sendForward(fwd, retryFlows);
1636 if (--attempts > 0) {
1637 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1638 }
1639 }
1640 }
1641
Saurav Das822c4e22015-10-23 10:51:11 -07001642}