blob: 890af99a787dcde4f0a5579a6d480c9efa245783 [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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 */
16package org.onosproject.driver.pipeline;
17
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
Charles Chan5b9df8d2016-03-28 22:21:40 -070019import com.google.common.collect.ImmutableSet;
Saurav Das822c4e22015-10-23 10:51:11 -070020import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070021import org.onlab.packet.Ethernet;
Charles Chan14967c22015-12-07 11:11:50 -080022import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070023import org.onlab.packet.MacAddress;
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;
60import org.onosproject.net.flow.criteria.VlanIdCriterion;
61import org.onosproject.net.flow.instructions.Instruction;
62import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
63import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080064import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070065import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080066import org.onosproject.net.flow.instructions.L3ModificationInstruction;
67import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flowobjective.FilteringObjective;
69import org.onosproject.net.flowobjective.FlowObjectiveStore;
70import org.onosproject.net.flowobjective.ForwardingObjective;
71import org.onosproject.net.flowobjective.NextObjective;
72import org.onosproject.net.flowobjective.Objective;
73import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070074import org.onosproject.net.group.DefaultGroupKey;
75import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070078import org.onosproject.store.serializers.KryoNamespaces;
79import org.slf4j.Logger;
80
Pier Ventree0ae7a32016-11-23 09:57:42 -080081import java.util.ArrayDeque;
82import java.util.ArrayList;
83import java.util.Collection;
84import java.util.Collections;
85import java.util.Deque;
86import java.util.List;
87import java.util.Objects;
88import java.util.Set;
89import java.util.concurrent.ConcurrentHashMap;
90import java.util.concurrent.ScheduledExecutorService;
91import java.util.concurrent.TimeUnit;
92
93import static java.util.concurrent.Executors.newScheduledThreadPool;
94import static org.onlab.util.Tools.groupedThreads;
95import static org.slf4j.LoggerFactory.getLogger;
96
Saurav Das822c4e22015-10-23 10:51:11 -070097/**
98 * Driver for Broadcom's OF-DPA v2.0 TTP.
99 *
100 */
Charles Chan361154b2016-03-24 10:23:39 -0700101public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700102 protected static final int PORT_TABLE = 0;
103 protected static final int VLAN_TABLE = 10;
104 protected static final int TMAC_TABLE = 20;
105 protected static final int UNICAST_ROUTING_TABLE = 30;
106 protected static final int MULTICAST_ROUTING_TABLE = 40;
107 protected static final int MPLS_TABLE_0 = 23;
108 protected static final int MPLS_TABLE_1 = 24;
Charles Chancad338a2016-09-16 18:03:11 -0700109 protected static final int MPLS_L3_TYPE = 27;
Saurav Das822c4e22015-10-23 10:51:11 -0700110 protected static final int BRIDGING_TABLE = 50;
111 protected static final int ACL_TABLE = 60;
112 protected static final int MAC_LEARNING_TABLE = 254;
113 protected static final long OFPP_MAX = 0xffffff00L;
114
Saurav Das52025962016-01-28 22:30:01 -0800115 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800116 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700117 protected static final int LOWEST_PRIORITY = 0x0;
118
Saurav Das822c4e22015-10-23 10:51:11 -0700119 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700120 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700121 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700122 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800123 protected GroupService groupService;
124 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700125 protected DeviceId deviceId;
126 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700127 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800128 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700129 .register(KryoNamespaces.API)
130 .register(GroupKey.class)
131 .register(DefaultGroupKey.class)
Charles Chan361154b2016-03-24 10:23:39 -0700132 .register(Ofdpa2GroupHandler.OfdpaNextGroup.class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800133 .register(ArrayDeque.class)
Charles Chaneefdedf2016-05-23 16:45:45 -0700134 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700135
Charles Chan425854b2016-04-11 15:32:12 -0700136 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700137
Saurav Das52025962016-01-28 22:30:01 -0800138 protected Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800139 new ConcurrentHashMap<>());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700140 // flows installations to be retried
141 protected ScheduledExecutorService executorService
142 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
143 protected static final int MAX_RETRY_ATTEMPTS = 10;
144 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800145
Saurav Das822c4e22015-10-23 10:51:11 -0700146 @Override
147 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700148 this.deviceId = deviceId;
149
Charles Chan188ebf52015-12-23 00:15:11 -0800150 // Initialize OFDPA group handler
Charles Chan425854b2016-04-11 15:32:12 -0700151 groupHandler = new Ofdpa2GroupHandler();
152 groupHandler.init(deviceId, context);
Saurav Das822c4e22015-10-23 10:51:11 -0700153
Charles Chan425854b2016-04-11 15:32:12 -0700154 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700155 coreService = serviceDirectory.get(CoreService.class);
156 flowRuleService = serviceDirectory.get(FlowRuleService.class);
157 groupService = serviceDirectory.get(GroupService.class);
158 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700159 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700160
161 driverId = coreService.registerApplication(
Charles Chan425854b2016-04-11 15:32:12 -0700162 "org.onosproject.driver.Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700163
Saurav Das822c4e22015-10-23 10:51:11 -0700164 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700165 }
166
167 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800168 // OF-DPA does not require initializing the pipeline as it puts default
169 // rules automatically in the hardware. However emulation of OFDPA in
170 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700171 }
172
173 //////////////////////////////////////
174 // Flow Objectives
175 //////////////////////////////////////
176
177 @Override
178 public void filter(FilteringObjective filteringObjective) {
179 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
180 processFilter(filteringObjective,
181 filteringObjective.op() == Objective.Operation.ADD,
182 filteringObjective.appId());
183 } else {
184 // Note that packets that don't match the PERMIT filter are
185 // automatically denied. The DENY filter is used to deny packets
186 // that are otherwise permitted by the PERMIT filter.
187 // Use ACL table flow rules here for DENY filtering objectives
188 log.debug("filter objective other than PERMIT currently not supported");
189 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
190 }
191 }
192
193 @Override
194 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700195 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700196 if (rules == null || rules.isEmpty()) {
197 // Assumes fail message has already been generated to the objective
198 // context. Returning here prevents spurious pass message to be
199 // generated by FlowRule service for empty flowOps.
200 return;
201 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700202 sendForward(fwd, rules);
203 }
204
205 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
206 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700207 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700208 case ADD:
209 rules.stream()
210 .filter(Objects::nonNull)
211 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800212 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700213 break;
214 case REMOVE:
215 rules.stream()
216 .filter(Objects::nonNull)
217 .forEach(flowOpsBuilder::remove);
218 break;
219 default:
220 fail(fwd, ObjectiveError.UNKNOWN);
221 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700222 }
223
Saurav Das822c4e22015-10-23 10:51:11 -0700224 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
225 @Override
226 public void onSuccess(FlowRuleOperations ops) {
227 pass(fwd);
228 }
229
230 @Override
231 public void onError(FlowRuleOperations ops) {
232 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
233 }
234 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700235 }
236
237 @Override
238 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800239 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
240 switch (nextObjective.op()) {
241 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800242 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800243 log.warn("Cannot add next {} that already exists in device {}",
244 nextObjective.id(), deviceId);
245 return;
246 }
247 log.debug("Processing NextObjective id{} in dev{} - add group",
248 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700249 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800250 break;
251 case ADD_TO_EXISTING:
252 if (nextGroup != null) {
253 log.debug("Processing NextObjective id{} in dev{} - add bucket",
254 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700255 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800256 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800257 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800258 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
259 nextObjective.id(), deviceId);
260 // by design only one pending bucket is allowed for the group
Charles Chan425854b2016-04-11 15:32:12 -0700261 groupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800262 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800263 break;
264 case REMOVE:
265 if (nextGroup == null) {
266 log.warn("Cannot remove next {} that does not exist in device {}",
267 nextObjective.id(), deviceId);
268 return;
269 }
270 log.debug("Processing NextObjective id{} in dev{} - remove group",
271 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700272 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800273 break;
274 case REMOVE_FROM_EXISTING:
275 if (nextGroup == null) {
276 log.warn("Cannot remove from next {} that does not exist in device {}",
277 nextObjective.id(), deviceId);
278 return;
279 }
280 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
281 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700282 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800283 break;
284 default:
Saurav Das4f980082015-11-05 13:39:15 -0800285 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700286 }
287 }
288
289 //////////////////////////////////////
290 // Flow handling
291 //////////////////////////////////////
292
293 /**
294 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
295 * and IP addresses configured on switch ports happen in different tables.
296 * Note that IP filtering rules need to be added to the ACL table, as there
297 * is no mechanism to send to controller via IP table.
298 *
299 * @param filt the filtering objective
300 * @param install indicates whether to add or remove the objective
301 * @param applicationId the application that sent this objective
302 */
Saurav Das52025962016-01-28 22:30:01 -0800303 protected void processFilter(FilteringObjective filt,
304 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700305 // This driver only processes filtering criteria defined with switch
306 // ports as the key
307 PortCriterion portCriterion = null;
308 EthCriterion ethCriterion = null;
309 VlanIdCriterion vidCriterion = null;
310 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
311 if (!filt.key().equals(Criteria.dummy()) &&
312 filt.key().type() == Criterion.Type.IN_PORT) {
313 portCriterion = (PortCriterion) filt.key();
314 } else {
315 log.warn("No key defined in filtering objective from app: {}. Not"
316 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700317 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700318 return;
319 }
Saurav Das59232cf2016-04-27 18:35:50 -0700320 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
321 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700322 // convert filtering conditions for switch-intfs into flowrules
323 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
324 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700325 if (criterion.type() == Criterion.Type.ETH_DST ||
326 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das822c4e22015-10-23 10:51:11 -0700327 ethCriterion = (EthCriterion) criterion;
328 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
329 vidCriterion = (VlanIdCriterion) criterion;
330 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
331 ips.add((IPCriterion) criterion);
332 } else {
333 log.error("Unsupported filter {}", criterion);
334 fail(filt, ObjectiveError.UNSUPPORTED);
335 return;
336 }
337 }
338
Saurav Das0e99e2b2015-10-28 12:39:42 -0700339 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800340 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700341 // Use the VLAN in metadata whenever a metadata is provided
342 if (filt.meta() != null) {
343 assignedVlan = readVlanFromTreatment(filt.meta());
344 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
345 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800346 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700347 }
Charles Chane849c192016-01-11 18:28:54 -0800348
Charles Chand55e84d2016-03-30 17:54:24 -0700349 if (assignedVlan == null) {
350 log.error("Driver fails to extract VLAN information. "
351 + "Not proccessing VLAN filters on device {}.", deviceId);
352 log.debug("VLAN ID in criterion={}, metadata={}",
353 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
354 fail(filt, ObjectiveError.BADPARAMS);
355 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700356 }
357 }
358
Charles Chane849c192016-01-11 18:28:54 -0800359 if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700360 // NOTE: it is possible that a filtering objective only has vidCriterion
361 log.debug("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700362 } else {
363 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700364 vidCriterion, assignedVlan,
365 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700366 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
367 tmacRule, deviceId);
368 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
369 }
370 }
371
Charles Chan985b12e2016-05-11 19:47:22 -0700372 if (vidCriterion == null) {
373 // NOTE: it is possible that a filtering objective only has ethCriterion
374 log.debug("filtering objective missing dstMac or VLAN, "
Charles Chane849c192016-01-11 18:28:54 -0800375 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700376 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800377 /*
378 * NOTE: Separate vlan filtering rules and assignment rules
379 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800380 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800381 */
382 List<FlowRule> allRules = processVlanIdFilter(
383 portCriterion, vidCriterion, assignedVlan, applicationId);
384 List<FlowRule> filteringRules = new ArrayList<>();
385 List<FlowRule> assignmentRules = new ArrayList<>();
386
387 allRules.forEach(flowRule -> {
388 ExtensionCriterion extCriterion =
389 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
390 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
Charles Chanbe8aea42016-02-24 12:04:47 -0800391 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800392 filteringRules.add(flowRule);
393 } else {
394 assignmentRules.add(flowRule);
395 }
396 });
397
398 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700399 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800400 filteringRule, deviceId);
401 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
402 }
403
404 ops.newStage();
405
406 for (FlowRule assignmentRule : assignmentRules) {
407 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
408 assignmentRule, deviceId);
409 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700410 }
411 }
412
413 for (IPCriterion ipaddr : ips) {
414 // since we ignore port information for IP rules, and the same (gateway) IP
415 // can be configured on multiple ports, we make sure that we send
416 // only a single rule to the switch.
417 if (!sentIpFilters.contains(ipaddr)) {
418 sentIpFilters.add(ipaddr);
419 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
420 ipaddr, deviceId);
421 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
422 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
423 selector.matchEthType(Ethernet.TYPE_IPV4);
424 selector.matchIPDst(ipaddr.ip());
425 treatment.setOutput(PortNumber.CONTROLLER);
426 FlowRule rule = DefaultFlowRule.builder()
427 .forDevice(deviceId)
428 .withSelector(selector.build())
429 .withTreatment(treatment.build())
430 .withPriority(HIGHEST_PRIORITY)
431 .fromApp(applicationId)
432 .makePermanent()
433 .forTable(ACL_TABLE).build();
434 ops = install ? ops.add(rule) : ops.remove(rule);
435 }
436 }
437
438 // apply filtering flow rules
439 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
440 @Override
441 public void onSuccess(FlowRuleOperations ops) {
442 log.info("Applied {} filtering rules in device {}",
443 ops.stages().get(0).size(), deviceId);
444 pass(filt);
445 }
446
447 @Override
448 public void onError(FlowRuleOperations ops) {
449 log.info("Failed to apply all filtering rules in dev {}", deviceId);
450 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
451 }
452 }));
453
454 }
455
456 /**
457 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700458 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700459 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700460 *
Charles Chanf9e98652016-09-07 16:54:23 -0700461 * @param portCriterion port on device for which this filter is programmed
462 * @param vidCriterion vlan assigned to port, or NONE for untagged
463 * @param assignedVlan assigned vlan-id for untagged packets
464 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700465 * @return list of FlowRule for port-vlan filters
466 */
467 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
468 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700469 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700470 ApplicationId applicationId) {
Charles Chanf9e98652016-09-07 16:54:23 -0700471 return processVlanIdFilterInternal(portCriterion, vidCriterion, assignedVlan,
472 applicationId, true);
473 }
474
475 /**
476 * Internal implementation of processVlanIdFilter.
477 * <p>
478 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
479 * Since it is non-OF spec, we need an extension treatment for that.
480 * The useSetVlanExtension must be set to false for OFDPA i12.
481 * </p>
482 *
483 * @param portCriterion port on device for which this filter is programmed
484 * @param vidCriterion vlan assigned to port, or NONE for untagged
485 * @param assignedVlan assigned vlan-id for untagged packets
486 * @param applicationId for application programming this filter
487 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
488 * @return list of FlowRule for port-vlan filters
489 */
490 protected List<FlowRule> processVlanIdFilterInternal(PortCriterion portCriterion,
491 VlanIdCriterion vidCriterion,
492 VlanId assignedVlan,
493 ApplicationId applicationId,
494 boolean useSetVlanExtension) {
Charles Chan14967c22015-12-07 11:11:50 -0800495 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700496 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
497 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800498 TrafficSelector.Builder preSelector = null;
499 TrafficTreatment.Builder preTreatment = null;
500
Saurav Das4f980082015-11-05 13:39:15 -0800501 treatment.transition(TMAC_TABLE);
502
Saurav Das822c4e22015-10-23 10:51:11 -0700503 if (vidCriterion.vlanId() == VlanId.NONE) {
504 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800505 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800506 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700507 if (useSetVlanExtension) {
508 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
509 treatment.extension(ofdpaSetVlanVid, deviceId);
510 } else {
511 treatment.setVlanId(assignedVlan);
512 }
Charles Chan14967c22015-12-07 11:11:50 -0800513
514 preSelector = DefaultTrafficSelector.builder();
515 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
516 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
517 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
518
Saurav Das4f980082015-11-05 13:39:15 -0800519 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800520 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
521 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700522
523 if (!assignedVlan.equals(vidCriterion.vlanId())) {
524 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
525 treatment.extension(ofdpaSetVlanVid, deviceId);
526 }
Saurav Das822c4e22015-10-23 10:51:11 -0700527 }
Saurav Das822c4e22015-10-23 10:51:11 -0700528
529 // ofdpa cannot match on ALL portnumber, so we need to use separate
530 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800531 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700532 if (portCriterion.port() == PortNumber.ALL) {
533 for (Port port : deviceService.getPorts(deviceId)) {
534 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
535 portnums.add(port.number());
536 }
537 }
538 } else {
539 portnums.add(portCriterion.port());
540 }
Saurav Das4f980082015-11-05 13:39:15 -0800541
Saurav Das822c4e22015-10-23 10:51:11 -0700542 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800543 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700544 selector.matchInPort(pnum);
545 FlowRule rule = DefaultFlowRule.builder()
546 .forDevice(deviceId)
547 .withSelector(selector.build())
548 .withTreatment(treatment.build())
549 .withPriority(DEFAULT_PRIORITY)
550 .fromApp(applicationId)
551 .makePermanent()
552 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800553
554 if (preSelector != null) {
555 preSelector.matchInPort(pnum);
556 FlowRule preRule = DefaultFlowRule.builder()
557 .forDevice(deviceId)
558 .withSelector(preSelector.build())
559 .withTreatment(preTreatment.build())
560 .withPriority(DEFAULT_PRIORITY)
561 .fromApp(applicationId)
562 .makePermanent()
563 .forTable(VLAN_TABLE).build();
564 rules.add(preRule);
565 }
566
Saurav Das822c4e22015-10-23 10:51:11 -0700567 rules.add(rule);
568 }
569 return rules;
570 }
571
572 /**
573 * Allows routed packets with correct destination MAC to be directed
574 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700575 *
576 * @param portCriterion port on device for which this filter is programmed
577 * @param ethCriterion dstMac of device for which is filter is programmed
578 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700579 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700580 * @param applicationId for application programming this filter
581 * @return list of FlowRule for port-vlan filters
582
583 */
584 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
585 EthCriterion ethCriterion,
586 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700587 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700588 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800589 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
590 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
591 return processEthDstOnlyFilter(ethCriterion, applicationId);
592 }
593
Charles Chan5b9df8d2016-03-28 22:21:40 -0700594 // Multicast MAC
595 if (ethCriterion.mask() != null) {
596 return processMcastEthDstFilter(ethCriterion, applicationId);
597 }
598
Saurav Das822c4e22015-10-23 10:51:11 -0700599 //handling untagged packets via assigned VLAN
600 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700601 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700602 }
603 // ofdpa cannot match on ALL portnumber, so we need to use separate
604 // rules for each port.
605 List<PortNumber> portnums = new ArrayList<PortNumber>();
606 if (portCriterion.port() == PortNumber.ALL) {
607 for (Port port : deviceService.getPorts(deviceId)) {
608 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
609 portnums.add(port.number());
610 }
611 }
612 } else {
613 portnums.add(portCriterion.port());
614 }
615
616 List<FlowRule> rules = new ArrayList<FlowRule>();
617 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800618 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700619 // for unicast IP packets
620 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
621 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
622 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800623 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700624 selector.matchEthType(Ethernet.TYPE_IPV4);
625 selector.matchEthDst(ethCriterion.mac());
626 treatment.transition(UNICAST_ROUTING_TABLE);
627 FlowRule rule = DefaultFlowRule.builder()
628 .forDevice(deviceId)
629 .withSelector(selector.build())
630 .withTreatment(treatment.build())
631 .withPriority(DEFAULT_PRIORITY)
632 .fromApp(applicationId)
633 .makePermanent()
634 .forTable(TMAC_TABLE).build();
635 rules.add(rule);
636 //for MPLS packets
637 selector = DefaultTrafficSelector.builder();
638 treatment = DefaultTrafficTreatment.builder();
639 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800640 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700641 selector.matchEthType(Ethernet.MPLS_UNICAST);
642 selector.matchEthDst(ethCriterion.mac());
643 treatment.transition(MPLS_TABLE_0);
644 rule = DefaultFlowRule.builder()
645 .forDevice(deviceId)
646 .withSelector(selector.build())
647 .withTreatment(treatment.build())
648 .withPriority(DEFAULT_PRIORITY)
649 .fromApp(applicationId)
650 .makePermanent()
651 .forTable(TMAC_TABLE).build();
652 rules.add(rule);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800653 /*
654 * TMAC rules for IPv6 packets
655 */
656 selector = DefaultTrafficSelector.builder();
657 treatment = DefaultTrafficTreatment.builder();
658 selector.matchInPort(pnum);
659 selector.extension(ofdpaMatchVlanVid, deviceId);
660 selector.matchEthType(Ethernet.TYPE_IPV6);
661 selector.matchEthDst(ethCriterion.mac());
662 treatment.transition(UNICAST_ROUTING_TABLE);
663 rule = DefaultFlowRule.builder()
664 .forDevice(deviceId)
665 .withSelector(selector.build())
666 .withTreatment(treatment.build())
667 .withPriority(DEFAULT_PRIORITY)
668 .fromApp(applicationId)
669 .makePermanent()
670 .forTable(TMAC_TABLE).build();
671 rules.add(rule);
Saurav Das822c4e22015-10-23 10:51:11 -0700672 }
673 return rules;
674 }
675
Charles Chan5270ed02016-01-30 23:22:37 -0800676 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
677 ApplicationId applicationId) {
678 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
679 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
680 selector.matchEthType(Ethernet.TYPE_IPV4);
681 selector.matchEthDst(ethCriterion.mac());
682 treatment.transition(UNICAST_ROUTING_TABLE);
683 FlowRule rule = DefaultFlowRule.builder()
684 .forDevice(deviceId)
685 .withSelector(selector.build())
686 .withTreatment(treatment.build())
687 .withPriority(DEFAULT_PRIORITY)
688 .fromApp(applicationId)
689 .makePermanent()
690 .forTable(TMAC_TABLE).build();
691 return ImmutableList.<FlowRule>builder().add(rule).build();
692 }
693
Charles Chan5b9df8d2016-03-28 22:21:40 -0700694 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
695 ApplicationId applicationId) {
696 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
697 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
698 selector.matchEthType(Ethernet.TYPE_IPV4);
699 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
700 treatment.transition(MULTICAST_ROUTING_TABLE);
701 FlowRule rule = DefaultFlowRule.builder()
702 .forDevice(deviceId)
703 .withSelector(selector.build())
704 .withTreatment(treatment.build())
705 .withPriority(DEFAULT_PRIORITY)
706 .fromApp(applicationId)
707 .makePermanent()
708 .forTable(TMAC_TABLE).build();
709 return ImmutableList.<FlowRule>builder().add(rule).build();
710 }
711
Saurav Das822c4e22015-10-23 10:51:11 -0700712 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
713 switch (fwd.flag()) {
714 case SPECIFIC:
715 return processSpecific(fwd);
716 case VERSATILE:
717 return processVersatile(fwd);
718 default:
719 fail(fwd, ObjectiveError.UNKNOWN);
720 log.warn("Unknown forwarding flag {}", fwd.flag());
721 }
722 return Collections.emptySet();
723 }
724
Pier Ventree0ae7a32016-11-23 09:57:42 -0800725
726
Saurav Das822c4e22015-10-23 10:51:11 -0700727 /**
728 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
729 * ACL table.
730 * @param fwd the forwarding objective of type 'versatile'
731 * @return a collection of flow rules to be sent to the switch. An empty
732 * collection may be returned if there is a problem in processing
733 * the flow rule
734 */
Saurav Das52025962016-01-28 22:30:01 -0800735 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800736 log.info("Processing versatile forwarding objective:{} in dev:{}",
737 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700738
739 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800740 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700741 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800742 log.error("Versatile forwarding objective:{} must include ethType",
743 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700744 fail(fwd, ObjectiveError.BADPARAMS);
745 return Collections.emptySet();
746 }
747 if (fwd.nextId() == null && fwd.treatment() == null) {
748 log.error("Forwarding objective {} from {} must contain "
749 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800750 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700751 return Collections.emptySet();
752 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800753
Saurav Das77b5e902016-01-27 17:01:59 -0800754 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
755 fwd.selector().criteria().forEach(criterion -> {
756 if (criterion instanceof VlanIdCriterion) {
757 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
758 // ensure that match does not include vlan = NONE as OF-DPA does not
759 // match untagged packets this way in the ACL table.
760 if (vlanId.equals(VlanId.NONE)) {
761 return;
762 }
763 OfdpaMatchVlanVid ofdpaMatchVlanVid =
764 new OfdpaMatchVlanVid(vlanId);
765 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800766 } else if (criterion instanceof Icmpv6TypeCriterion ||
767 criterion instanceof Icmpv6CodeCriterion) {
768 /*
769 * We silenty discard these criterions, our current
770 * OFDPA platform does not support these matches on
771 * the ACL table.
772 */
773 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800774 } else {
775 sbuilder.add(criterion);
776 }
777 });
778
Saurav Das822c4e22015-10-23 10:51:11 -0700779 // XXX driver does not currently do type checking as per Tables 65-67 in
780 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800781 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
782 if (fwd.treatment() != null) {
783 for (Instruction ins : fwd.treatment().allInstructions()) {
784 if (ins instanceof OutputInstruction) {
785 OutputInstruction o = (OutputInstruction) ins;
786 if (o.port() == PortNumber.CONTROLLER) {
787 ttBuilder.add(o);
788 } else {
789 log.warn("Only allowed treatments in versatile forwarding "
790 + "objectives are punts to the controller");
791 }
792 } else {
793 log.warn("Cannot process instruction in versatile fwd {}", ins);
794 }
Saurav Das822c4e22015-10-23 10:51:11 -0700795 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800796 if (fwd.treatment().clearedDeferred()) {
797 ttBuilder.wipeDeferred();
798 }
Saurav Das822c4e22015-10-23 10:51:11 -0700799 }
Saurav Das822c4e22015-10-23 10:51:11 -0700800 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800801 // overide case
802 NextGroup next = getGroupForNextObjective(fwd.nextId());
803 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
804 // we only need the top level group's key to point the flow to it
805 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
806 if (group == null) {
807 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
808 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
809 fail(fwd, ObjectiveError.GROUPMISSING);
810 return Collections.emptySet();
811 }
812 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700813 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800814
815 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
816 .fromApp(fwd.appId())
817 .withPriority(fwd.priority())
818 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800819 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800820 .withTreatment(ttBuilder.build())
821 .makePermanent()
822 .forTable(ACL_TABLE);
823 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700824 }
825
826 /**
827 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800828 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700829 *
830 * @param fwd the forwarding objective of type 'specific'
831 * @return a collection of flow rules. Typically there will be only one
832 * for this type of forwarding objective. An empty set may be
833 * returned if there is an issue in processing the objective.
834 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800835 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700836 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800837 fwd.id(), deviceId, fwd.nextId());
838 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
839 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
840
841 if (isEthTypeObj) {
842 return processEthTypeSpecific(fwd);
843 } else if (isEthDstObj) {
844 return processEthDstSpecific(fwd);
845 } else {
846 log.warn("processSpecific: Unsupported forwarding objective "
847 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700848 fail(fwd, ObjectiveError.UNSUPPORTED);
849 return Collections.emptySet();
850 }
Saurav Das4ce45962015-11-24 23:21:05 -0800851 }
852
Saurav Das4ce45962015-11-24 23:21:05 -0800853 /**
854 * Handles forwarding rules to the IP and MPLS tables.
855 *
856 * @param fwd the forwarding objective
857 * @return A collection of flow rules, or an empty set
858 */
859 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700860 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700861 }
862
863 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800864 * Helper method to build Ipv6 selector using the selector provided by
865 * a forwarding objective.
866 *
867 * @param builderToUpdate the builder to update
868 * @param fwd the selector to read
869 * @return 0 if the update ends correctly. -1 if the matches
870 * are not yet supported
871 */
872 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
873 ForwardingObjective fwd) {
874
875 TrafficSelector selector = fwd.selector();
876
877 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
878 if (ipv6Dst.isMulticast()) {
879 log.warn("IPv6 Multicast is currently not supported");
880 fail(fwd, ObjectiveError.BADPARAMS);
881 return -1;
882 } else {
883 if (ipv6Dst.prefixLength() == 0) {
884 log.warn("Default ipv6 route is currently not supported");
885 fail(fwd, ObjectiveError.BADPARAMS);
886 return -1;
887 } else {
888 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
889 }
890 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
891 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
892 }
893 return 0;
894 }
895
896 /**
Charles Chanf9e98652016-09-07 16:54:23 -0700897 * Internal implementation of processEthTypeSpecific.
898 * <p>
899 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
900 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
901 * The allowDefaultRoute must be set to false for OFDPA i12.
902 * </p>
903 *
904 * @param fwd the forwarding objective
905 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -0800906 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -0700907 * @return A collection of flow rules, or an empty set
908 */
909 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -0700910 boolean allowDefaultRoute,
911 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -0800912 TrafficSelector selector = fwd.selector();
913 EthTypeCriterion ethType =
914 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800915 boolean defaultRule = false;
916 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700917 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800918 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800919 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800920 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800921 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800922
Saurav Das8a0732e2015-11-20 15:27:53 -0800923 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800924 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700925 if (ipv4Dst.isMulticast()) {
926 if (ipv4Dst.prefixLength() != 32) {
927 log.warn("Multicast specific forwarding objective can only be /32");
928 fail(fwd, ObjectiveError.BADPARAMS);
929 return ImmutableSet.of();
930 }
931 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
932 if (assignedVlan == null) {
933 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
934 fail(fwd, ObjectiveError.BADPARAMS);
935 return ImmutableSet.of();
936 }
937 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
938 filteredSelector.extension(ofdpaMatchVlanVid, deviceId);
939 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
940 forTableId = MULTICAST_ROUTING_TABLE;
941 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
942 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800943 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700944 if (ipv4Dst.prefixLength() == 0) {
945 if (allowDefaultRoute) {
946 // The entire IPV4_DST field is wildcarded intentionally
947 filteredSelector.matchEthType(Ethernet.TYPE_IPV4);
948 } else {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800949 // NOTE: The switch does not support matching 0.0.0.0/0
950 // Split it into 0.0.0.0/1 and 128.0.0.0/1
Charles Chanf9e98652016-09-07 16:54:23 -0700951 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
952 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
953 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
954 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
955 defaultRule = true;
956 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700957 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700958 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700959 }
960 forTableId = UNICAST_ROUTING_TABLE;
961 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
962 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800963 }
Charles Chan14967c22015-12-07 11:11:50 -0800964
965 if (fwd.treatment() != null) {
966 for (Instruction instr : fwd.treatment().allInstructions()) {
967 if (instr instanceof L3ModificationInstruction &&
968 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -0800969 // XXX decrementing IP ttl is done automatically for routing, this
970 // action is ignored or rejected in ofdpa as it is not fully implemented
971 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -0800972 }
973 }
974 }
975
Pier Ventree0ae7a32016-11-23 09:57:42 -0800976 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
977 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
978 return Collections.emptyList();
979 }
980 forTableId = UNICAST_ROUTING_TABLE;
981 if (fwd.treatment() != null) {
982 for (Instruction instr : fwd.treatment().allInstructions()) {
983 if (instr instanceof L3ModificationInstruction &&
984 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
985 // XXX decrementing IP ttl is done automatically for routing, this
986 // action is ignored or rejected in ofdpa as it is not fully implemented
987 //tb.deferred().add(instr);
988 }
989 }
990 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800991 } else {
992 filteredSelector
993 .matchEthType(Ethernet.MPLS_UNICAST)
994 .matchMplsLabel(((MplsCriterion)
995 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
996 MplsBosCriterion bos = (MplsBosCriterion) selector
997 .getCriterion(Criterion.Type.MPLS_BOS);
998 if (bos != null) {
999 filteredSelector.matchMplsBos(bos.mplsBos());
1000 }
1001 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001002 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1003 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001004
Charles Chan14967c22015-12-07 11:11:50 -08001005 if (fwd.treatment() != null) {
1006 for (Instruction instr : fwd.treatment().allInstructions()) {
1007 if (instr instanceof L2ModificationInstruction &&
1008 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001009 popMpls = true;
Saurav Das9c705342017-01-06 11:36:01 -08001010 // OF-DPA does not pop in MPLS table. Instead it requires
1011 // setting the MPLS_TYPE so pop can happen down the pipeline
Charles Chan14967c22015-12-07 11:11:50 -08001012 }
1013 if (instr instanceof L3ModificationInstruction &&
1014 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1015 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1016 tb.immediate().decMplsTtl();
1017 }
1018 if (instr instanceof L3ModificationInstruction &&
1019 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1020 tb.immediate().add(instr);
1021 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001022 }
1023 }
1024 }
Saurav Das822c4e22015-10-23 10:51:11 -07001025
1026 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001027 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1028 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001029 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1030 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001031 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1032 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1033 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001034 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001035 return Collections.emptySet();
1036 }
1037
Saurav Das423fe2b2015-12-04 10:52:59 -08001038 NextGroup next = getGroupForNextObjective(fwd.nextId());
1039 if (next != null) {
1040 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1041 // we only need the top level group's key to point the flow to it
1042 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1043 if (group == null) {
1044 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1045 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1046 fail(fwd, ObjectiveError.GROUPMISSING);
1047 return Collections.emptySet();
1048 }
1049 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001050 // check if group is empty
1051 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1052 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1053 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1054 emptyGroup = true;
1055 }
Saurav Das25190812016-05-27 13:54:07 -07001056 } else {
1057 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1058 fwd.nextId(), deviceId, fwd.id());
1059 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1060 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001061 }
Saurav Das822c4e22015-10-23 10:51:11 -07001062 }
Charles Chancad338a2016-09-16 18:03:11 -07001063
1064 if (forTableId == MPLS_TABLE_1) {
1065 if (mplsNextTable == MPLS_L3_TYPE) {
1066 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001067 // set mpls type as apply_action
1068 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001069 }
1070 tb.transition(mplsNextTable);
1071 } else {
1072 tb.transition(ACL_TABLE);
1073 }
1074
Saurav Das822c4e22015-10-23 10:51:11 -07001075 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1076 .fromApp(fwd.appId())
1077 .withPriority(fwd.priority())
1078 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001079 .withSelector(filteredSelector.build())
1080 .withTreatment(tb.build())
1081 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001082
1083 if (fwd.permanent()) {
1084 ruleBuilder.makePermanent();
1085 } else {
1086 ruleBuilder.makeTemporary(fwd.timeout());
1087 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001088 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1089 flowRuleCollection.add(ruleBuilder.build());
1090 if (defaultRule) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001091 flowRuleCollection.add(
1092 defaultRoute(fwd, complementarySelector, forTableId, tb)
1093 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001094 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1095 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001096 // XXX retrying flows may be necessary due to bug CORD-554
1097 if (emptyGroup) {
1098 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1099 RETRY_MS, TimeUnit.MILLISECONDS);
1100 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001101 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001102 }
1103
Pier Ventree0ae7a32016-11-23 09:57:42 -08001104 protected FlowRule defaultRoute(ForwardingObjective fwd,
1105 TrafficSelector.Builder complementarySelector,
1106 int forTableId,
1107 TrafficTreatment.Builder tb) {
1108 FlowRule.Builder rule = DefaultFlowRule.builder()
1109 .fromApp(fwd.appId())
1110 .withPriority(fwd.priority())
1111 .forDevice(deviceId)
1112 .withSelector(complementarySelector.build())
1113 .withTreatment(tb.build())
1114 .forTable(forTableId);
1115 if (fwd.permanent()) {
1116 rule.makePermanent();
1117 } else {
1118 rule.makeTemporary(fwd.timeout());
1119 }
1120 return rule.build();
1121 }
1122
Saurav Das4ce45962015-11-24 23:21:05 -08001123 /**
1124 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1125 * allowed in the bridging table - instead we use L2 Interface group or
1126 * L2 flood group
1127 *
1128 * @param fwd the forwarding objective
1129 * @return A collection of flow rules, or an empty set
1130 */
1131 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1132 List<FlowRule> rules = new ArrayList<>();
1133
1134 // Build filtered selector
1135 TrafficSelector selector = fwd.selector();
1136 EthCriterion ethCriterion = (EthCriterion) selector
1137 .getCriterion(Criterion.Type.ETH_DST);
1138 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1139 .getCriterion(Criterion.Type.VLAN_VID);
1140
1141 if (vlanIdCriterion == null) {
1142 log.warn("Forwarding objective for bridging requires vlan. Not "
1143 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1144 fail(fwd, ObjectiveError.BADPARAMS);
1145 return Collections.emptySet();
1146 }
1147
1148 TrafficSelector.Builder filteredSelectorBuilder =
1149 DefaultTrafficSelector.builder();
1150 // Do not match MacAddress for subnet broadcast entry
1151 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
1152 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1153 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1154 fwd.id(), fwd.nextId(), deviceId);
1155 } else {
1156 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1157 + "in dev:{} for vlan:{}",
1158 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1159 }
Charles Chan14967c22015-12-07 11:11:50 -08001160 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1161 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -08001162 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1163
1164 if (fwd.treatment() != null) {
1165 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1166 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1167 }
1168
1169 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1170 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001171 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001172 if (next != null) {
1173 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1174 // we only need the top level group's key to point the flow to it
1175 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1176 if (group != null) {
1177 treatmentBuilder.deferred().group(group.id());
1178 } else {
1179 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1180 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1181 fail(fwd, ObjectiveError.GROUPMISSING);
1182 return Collections.emptySet();
1183 }
1184 }
1185 }
1186 treatmentBuilder.immediate().transition(ACL_TABLE);
1187 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1188
1189 // Build bridging table entries
1190 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1191 flowRuleBuilder.fromApp(fwd.appId())
1192 .withPriority(fwd.priority())
1193 .forDevice(deviceId)
1194 .withSelector(filteredSelector)
1195 .withTreatment(filteredTreatment)
1196 .forTable(BRIDGING_TABLE);
1197 if (fwd.permanent()) {
1198 flowRuleBuilder.makePermanent();
1199 } else {
1200 flowRuleBuilder.makeTemporary(fwd.timeout());
1201 }
1202 rules.add(flowRuleBuilder.build());
1203 return rules;
1204 }
1205
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001206 //////////////////////////////////////
1207 // Helper Methods and Classes
1208 //////////////////////////////////////
1209
1210 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1211 TrafficSelector selector = fwd.selector();
1212 EthTypeCriterion ethType = (EthTypeCriterion) selector
1213 .getCriterion(Criterion.Type.ETH_TYPE);
1214 return !((ethType == null) ||
1215 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001216 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1217 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001218 }
1219
1220 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1221 TrafficSelector selector = fwd.selector();
1222 EthCriterion ethDst = (EthCriterion) selector
1223 .getCriterion(Criterion.Type.ETH_DST);
1224 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1225 .getCriterion(Criterion.Type.VLAN_VID);
1226 return !(ethDst == null && vlanId == null);
1227 }
1228
Saurav Das423fe2b2015-12-04 10:52:59 -08001229 protected NextGroup getGroupForNextObjective(Integer nextId) {
1230 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1231 if (next != null) {
1232 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1233 if (gkeys != null && !gkeys.isEmpty()) {
1234 return next;
1235 } else {
1236 log.warn("Empty next group found in FlowObjective store for "
1237 + "next-id:{} in dev:{}", nextId, deviceId);
1238 }
1239 } else {
1240 log.warn("next-id {} not found in Flow objective store for dev:{}",
1241 nextId, deviceId);
1242 }
1243 return null;
1244 }
1245
Charles Chan188ebf52015-12-23 00:15:11 -08001246 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001247 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001248 }
1249
Charles Chan188ebf52015-12-23 00:15:11 -08001250 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001251 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001252 }
Saurav Das24431192016-03-07 19:13:00 -08001253
Saurav Das24431192016-03-07 19:13:00 -08001254 @Override
1255 public List<String> getNextMappings(NextGroup nextGroup) {
1256 List<String> mappings = new ArrayList<>();
1257 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1258 for (Deque<GroupKey> gkd : gkeys) {
1259 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001260 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001261 for (GroupKey gk : gkd) {
1262 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001263 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001264 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001265 continue;
1266 }
1267 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1268 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001269 lastGroup = g;
1270 }
1271 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001272 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001273 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001274 lastGroupIns = lastGroup.buckets().buckets().get(0)
1275 .treatment().allInstructions();
1276 }
1277 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001278 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001279 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001280 }
1281 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001282 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001283 }
1284 return mappings;
1285 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001286
1287 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001288 if (selector == null) {
1289 return null;
1290 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001291 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1292 return (criterion == null)
1293 ? null : ((VlanIdCriterion) criterion).vlanId();
1294 }
1295
1296 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001297 if (selector == null) {
1298 return null;
1299 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001300 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1301 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1302 }
Charles Chand55e84d2016-03-30 17:54:24 -07001303
1304 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001305 if (treatment == null) {
1306 return null;
1307 }
Charles Chand55e84d2016-03-30 17:54:24 -07001308 for (Instruction i : treatment.allInstructions()) {
1309 if (i instanceof ModVlanIdInstruction) {
1310 return ((ModVlanIdInstruction) i).vlanId();
1311 }
1312 }
1313 return null;
1314 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001315
1316 /**
1317 * Utility class that retries sending flows a fixed number of times, even if
1318 * some of the attempts are successful. Used only for forwarding objectives.
1319 */
1320 protected final class RetryFlows implements Runnable {
1321 int attempts = MAX_RETRY_ATTEMPTS;
1322 private Collection<FlowRule> retryFlows;
1323 private ForwardingObjective fwd;
1324
1325 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1326 this.fwd = fwd;
1327 this.retryFlows = retryFlows;
1328 }
1329
1330 @Override
1331 public void run() {
1332 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1333 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1334 sendForward(fwd, retryFlows);
1335 if (--attempts > 0) {
1336 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1337 }
1338 }
1339 }
1340
Saurav Das822c4e22015-10-23 10:51:11 -07001341}