blob: cadca7514f35bc2f765ee1ef9f3c8e704feac7a9 [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 Chan425854b2016-04-11 15:32:12 -0700150 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700151 coreService = serviceDirectory.get(CoreService.class);
152 flowRuleService = serviceDirectory.get(FlowRuleService.class);
153 groupService = serviceDirectory.get(GroupService.class);
154 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700155 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700156
Charles Chan40132b32017-01-22 00:19:37 -0800157 initDriverId();
158 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700159
Saurav Das822c4e22015-10-23 10:51:11 -0700160 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700161 }
162
Charles Chan40132b32017-01-22 00:19:37 -0800163 protected void initDriverId() {
164 driverId = coreService.registerApplication(
165 "org.onosproject.driver.Ofdpa2Pipeline");
166 }
167
168 protected void initGroupHander(PipelinerContext context) {
169 groupHandler = new Ofdpa2GroupHandler();
170 groupHandler.init(deviceId, context);
171 }
172
Saurav Das822c4e22015-10-23 10:51:11 -0700173 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800174 // OF-DPA does not require initializing the pipeline as it puts default
175 // rules automatically in the hardware. However emulation of OFDPA in
176 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700177 }
178
179 //////////////////////////////////////
180 // Flow Objectives
181 //////////////////////////////////////
182
183 @Override
184 public void filter(FilteringObjective filteringObjective) {
185 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
186 processFilter(filteringObjective,
187 filteringObjective.op() == Objective.Operation.ADD,
188 filteringObjective.appId());
189 } else {
190 // Note that packets that don't match the PERMIT filter are
191 // automatically denied. The DENY filter is used to deny packets
192 // that are otherwise permitted by the PERMIT filter.
193 // Use ACL table flow rules here for DENY filtering objectives
194 log.debug("filter objective other than PERMIT currently not supported");
195 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
196 }
197 }
198
199 @Override
200 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700201 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700202 if (rules == null || rules.isEmpty()) {
203 // Assumes fail message has already been generated to the objective
204 // context. Returning here prevents spurious pass message to be
205 // generated by FlowRule service for empty flowOps.
206 return;
207 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700208 sendForward(fwd, rules);
209 }
210
211 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
212 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700213 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700214 case ADD:
215 rules.stream()
216 .filter(Objects::nonNull)
217 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800218 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700219 break;
220 case REMOVE:
221 rules.stream()
222 .filter(Objects::nonNull)
223 .forEach(flowOpsBuilder::remove);
224 break;
225 default:
226 fail(fwd, ObjectiveError.UNKNOWN);
227 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700228 }
229
Saurav Das822c4e22015-10-23 10:51:11 -0700230 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
231 @Override
232 public void onSuccess(FlowRuleOperations ops) {
233 pass(fwd);
234 }
235
236 @Override
237 public void onError(FlowRuleOperations ops) {
238 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
239 }
240 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700241 }
242
243 @Override
244 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800245 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
246 switch (nextObjective.op()) {
247 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800248 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800249 log.warn("Cannot add next {} that already exists in device {}",
250 nextObjective.id(), deviceId);
251 return;
252 }
253 log.debug("Processing NextObjective id{} in dev{} - add group",
254 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700255 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800256 break;
257 case ADD_TO_EXISTING:
258 if (nextGroup != null) {
259 log.debug("Processing NextObjective id{} in dev{} - add bucket",
260 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700261 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800262 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800263 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800264 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
265 nextObjective.id(), deviceId);
266 // by design only one pending bucket is allowed for the group
Charles Chan425854b2016-04-11 15:32:12 -0700267 groupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800268 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800269 break;
270 case REMOVE:
271 if (nextGroup == null) {
272 log.warn("Cannot remove next {} that does not exist in device {}",
273 nextObjective.id(), deviceId);
274 return;
275 }
276 log.debug("Processing NextObjective id{} in dev{} - remove group",
277 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700278 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800279 break;
280 case REMOVE_FROM_EXISTING:
281 if (nextGroup == null) {
282 log.warn("Cannot remove from next {} that does not exist in device {}",
283 nextObjective.id(), deviceId);
284 return;
285 }
286 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
287 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700288 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800289 break;
290 default:
Saurav Das4f980082015-11-05 13:39:15 -0800291 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700292 }
293 }
294
295 //////////////////////////////////////
296 // Flow handling
297 //////////////////////////////////////
298
299 /**
300 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
301 * and IP addresses configured on switch ports happen in different tables.
302 * Note that IP filtering rules need to be added to the ACL table, as there
303 * is no mechanism to send to controller via IP table.
304 *
305 * @param filt the filtering objective
306 * @param install indicates whether to add or remove the objective
307 * @param applicationId the application that sent this objective
308 */
Saurav Das52025962016-01-28 22:30:01 -0800309 protected void processFilter(FilteringObjective filt,
310 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700311 // This driver only processes filtering criteria defined with switch
312 // ports as the key
313 PortCriterion portCriterion = null;
314 EthCriterion ethCriterion = null;
315 VlanIdCriterion vidCriterion = null;
316 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
317 if (!filt.key().equals(Criteria.dummy()) &&
318 filt.key().type() == Criterion.Type.IN_PORT) {
319 portCriterion = (PortCriterion) filt.key();
320 } else {
321 log.warn("No key defined in filtering objective from app: {}. Not"
322 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700323 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700324 return;
325 }
Saurav Das59232cf2016-04-27 18:35:50 -0700326 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
327 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700328 // convert filtering conditions for switch-intfs into flowrules
329 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
330 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700331 if (criterion.type() == Criterion.Type.ETH_DST ||
332 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das822c4e22015-10-23 10:51:11 -0700333 ethCriterion = (EthCriterion) criterion;
334 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
335 vidCriterion = (VlanIdCriterion) criterion;
336 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
337 ips.add((IPCriterion) criterion);
338 } else {
339 log.error("Unsupported filter {}", criterion);
340 fail(filt, ObjectiveError.UNSUPPORTED);
341 return;
342 }
343 }
344
Saurav Das0e99e2b2015-10-28 12:39:42 -0700345 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800346 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700347 // Use the VLAN in metadata whenever a metadata is provided
348 if (filt.meta() != null) {
349 assignedVlan = readVlanFromTreatment(filt.meta());
350 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
351 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800352 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700353 }
Charles Chane849c192016-01-11 18:28:54 -0800354
Charles Chand55e84d2016-03-30 17:54:24 -0700355 if (assignedVlan == null) {
356 log.error("Driver fails to extract VLAN information. "
357 + "Not proccessing VLAN filters on device {}.", deviceId);
358 log.debug("VLAN ID in criterion={}, metadata={}",
359 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
360 fail(filt, ObjectiveError.BADPARAMS);
361 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700362 }
363 }
364
Charles Chane849c192016-01-11 18:28:54 -0800365 if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700366 // NOTE: it is possible that a filtering objective only has vidCriterion
367 log.debug("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700368 } else {
369 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700370 vidCriterion, assignedVlan,
371 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700372 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
373 tmacRule, deviceId);
374 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
375 }
376 }
377
Charles Chan985b12e2016-05-11 19:47:22 -0700378 if (vidCriterion == null) {
379 // NOTE: it is possible that a filtering objective only has ethCriterion
380 log.debug("filtering objective missing dstMac or VLAN, "
Charles Chane849c192016-01-11 18:28:54 -0800381 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700382 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800383 /*
384 * NOTE: Separate vlan filtering rules and assignment rules
385 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800386 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800387 */
388 List<FlowRule> allRules = processVlanIdFilter(
389 portCriterion, vidCriterion, assignedVlan, applicationId);
390 List<FlowRule> filteringRules = new ArrayList<>();
391 List<FlowRule> assignmentRules = new ArrayList<>();
392
393 allRules.forEach(flowRule -> {
394 ExtensionCriterion extCriterion =
395 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
396 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
Charles Chanbe8aea42016-02-24 12:04:47 -0800397 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800398 filteringRules.add(flowRule);
399 } else {
400 assignmentRules.add(flowRule);
401 }
402 });
403
404 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700405 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800406 filteringRule, deviceId);
407 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
408 }
409
410 ops.newStage();
411
412 for (FlowRule assignmentRule : assignmentRules) {
413 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
414 assignmentRule, deviceId);
415 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700416 }
417 }
418
419 for (IPCriterion ipaddr : ips) {
420 // since we ignore port information for IP rules, and the same (gateway) IP
421 // can be configured on multiple ports, we make sure that we send
422 // only a single rule to the switch.
423 if (!sentIpFilters.contains(ipaddr)) {
424 sentIpFilters.add(ipaddr);
425 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
426 ipaddr, deviceId);
427 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
428 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
429 selector.matchEthType(Ethernet.TYPE_IPV4);
430 selector.matchIPDst(ipaddr.ip());
431 treatment.setOutput(PortNumber.CONTROLLER);
432 FlowRule rule = DefaultFlowRule.builder()
433 .forDevice(deviceId)
434 .withSelector(selector.build())
435 .withTreatment(treatment.build())
436 .withPriority(HIGHEST_PRIORITY)
437 .fromApp(applicationId)
438 .makePermanent()
439 .forTable(ACL_TABLE).build();
440 ops = install ? ops.add(rule) : ops.remove(rule);
441 }
442 }
443
444 // apply filtering flow rules
445 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
446 @Override
447 public void onSuccess(FlowRuleOperations ops) {
448 log.info("Applied {} filtering rules in device {}",
449 ops.stages().get(0).size(), deviceId);
450 pass(filt);
451 }
452
453 @Override
454 public void onError(FlowRuleOperations ops) {
455 log.info("Failed to apply all filtering rules in dev {}", deviceId);
456 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
457 }
458 }));
459
460 }
461
462 /**
463 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700464 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700465 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700466 *
Charles Chanf9e98652016-09-07 16:54:23 -0700467 * @param portCriterion port on device for which this filter is programmed
468 * @param vidCriterion vlan assigned to port, or NONE for untagged
469 * @param assignedVlan assigned vlan-id for untagged packets
470 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700471 * @return list of FlowRule for port-vlan filters
472 */
473 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
474 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700475 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700476 ApplicationId applicationId) {
Charles Chanf9e98652016-09-07 16:54:23 -0700477 return processVlanIdFilterInternal(portCriterion, vidCriterion, assignedVlan,
478 applicationId, true);
479 }
480
481 /**
482 * Internal implementation of processVlanIdFilter.
483 * <p>
484 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
485 * Since it is non-OF spec, we need an extension treatment for that.
486 * The useSetVlanExtension must be set to false for OFDPA i12.
487 * </p>
488 *
489 * @param portCriterion port on device for which this filter is programmed
490 * @param vidCriterion vlan assigned to port, or NONE for untagged
491 * @param assignedVlan assigned vlan-id for untagged packets
492 * @param applicationId for application programming this filter
493 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
494 * @return list of FlowRule for port-vlan filters
495 */
496 protected List<FlowRule> processVlanIdFilterInternal(PortCriterion portCriterion,
497 VlanIdCriterion vidCriterion,
498 VlanId assignedVlan,
499 ApplicationId applicationId,
500 boolean useSetVlanExtension) {
Charles Chan14967c22015-12-07 11:11:50 -0800501 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700502 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
503 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800504 TrafficSelector.Builder preSelector = null;
505 TrafficTreatment.Builder preTreatment = null;
506
Saurav Das4f980082015-11-05 13:39:15 -0800507 treatment.transition(TMAC_TABLE);
508
Saurav Das822c4e22015-10-23 10:51:11 -0700509 if (vidCriterion.vlanId() == VlanId.NONE) {
510 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800511 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800512 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700513 if (useSetVlanExtension) {
514 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
515 treatment.extension(ofdpaSetVlanVid, deviceId);
516 } else {
517 treatment.setVlanId(assignedVlan);
518 }
Charles Chan14967c22015-12-07 11:11:50 -0800519
520 preSelector = DefaultTrafficSelector.builder();
521 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
522 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
523 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
524
Saurav Das4f980082015-11-05 13:39:15 -0800525 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800526 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
527 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700528
529 if (!assignedVlan.equals(vidCriterion.vlanId())) {
530 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
531 treatment.extension(ofdpaSetVlanVid, deviceId);
532 }
Saurav Das822c4e22015-10-23 10:51:11 -0700533 }
Saurav Das822c4e22015-10-23 10:51:11 -0700534
535 // ofdpa cannot match on ALL portnumber, so we need to use separate
536 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800537 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700538 if (portCriterion.port() == PortNumber.ALL) {
539 for (Port port : deviceService.getPorts(deviceId)) {
540 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
541 portnums.add(port.number());
542 }
543 }
544 } else {
545 portnums.add(portCriterion.port());
546 }
Saurav Das4f980082015-11-05 13:39:15 -0800547
Saurav Das822c4e22015-10-23 10:51:11 -0700548 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800549 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700550 selector.matchInPort(pnum);
551 FlowRule rule = DefaultFlowRule.builder()
552 .forDevice(deviceId)
553 .withSelector(selector.build())
554 .withTreatment(treatment.build())
555 .withPriority(DEFAULT_PRIORITY)
556 .fromApp(applicationId)
557 .makePermanent()
558 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800559
560 if (preSelector != null) {
561 preSelector.matchInPort(pnum);
562 FlowRule preRule = DefaultFlowRule.builder()
563 .forDevice(deviceId)
564 .withSelector(preSelector.build())
565 .withTreatment(preTreatment.build())
566 .withPriority(DEFAULT_PRIORITY)
567 .fromApp(applicationId)
568 .makePermanent()
569 .forTable(VLAN_TABLE).build();
570 rules.add(preRule);
571 }
572
Saurav Das822c4e22015-10-23 10:51:11 -0700573 rules.add(rule);
574 }
575 return rules;
576 }
577
578 /**
579 * Allows routed packets with correct destination MAC to be directed
580 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700581 *
582 * @param portCriterion port on device for which this filter is programmed
583 * @param ethCriterion dstMac of device for which is filter is programmed
584 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700585 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700586 * @param applicationId for application programming this filter
587 * @return list of FlowRule for port-vlan filters
588
589 */
590 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
591 EthCriterion ethCriterion,
592 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700593 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700594 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800595 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
596 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
597 return processEthDstOnlyFilter(ethCriterion, applicationId);
598 }
599
Charles Chan5b9df8d2016-03-28 22:21:40 -0700600 // Multicast MAC
601 if (ethCriterion.mask() != null) {
602 return processMcastEthDstFilter(ethCriterion, applicationId);
603 }
604
Saurav Das822c4e22015-10-23 10:51:11 -0700605 //handling untagged packets via assigned VLAN
606 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700607 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700608 }
609 // ofdpa cannot match on ALL portnumber, so we need to use separate
610 // rules for each port.
611 List<PortNumber> portnums = new ArrayList<PortNumber>();
612 if (portCriterion.port() == PortNumber.ALL) {
613 for (Port port : deviceService.getPorts(deviceId)) {
614 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
615 portnums.add(port.number());
616 }
617 }
618 } else {
619 portnums.add(portCriterion.port());
620 }
621
622 List<FlowRule> rules = new ArrayList<FlowRule>();
623 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800624 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700625 // for unicast IP packets
626 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
627 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
628 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800629 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700630 selector.matchEthType(Ethernet.TYPE_IPV4);
631 selector.matchEthDst(ethCriterion.mac());
632 treatment.transition(UNICAST_ROUTING_TABLE);
633 FlowRule rule = DefaultFlowRule.builder()
634 .forDevice(deviceId)
635 .withSelector(selector.build())
636 .withTreatment(treatment.build())
637 .withPriority(DEFAULT_PRIORITY)
638 .fromApp(applicationId)
639 .makePermanent()
640 .forTable(TMAC_TABLE).build();
641 rules.add(rule);
642 //for MPLS packets
643 selector = DefaultTrafficSelector.builder();
644 treatment = DefaultTrafficTreatment.builder();
645 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800646 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700647 selector.matchEthType(Ethernet.MPLS_UNICAST);
648 selector.matchEthDst(ethCriterion.mac());
649 treatment.transition(MPLS_TABLE_0);
650 rule = DefaultFlowRule.builder()
651 .forDevice(deviceId)
652 .withSelector(selector.build())
653 .withTreatment(treatment.build())
654 .withPriority(DEFAULT_PRIORITY)
655 .fromApp(applicationId)
656 .makePermanent()
657 .forTable(TMAC_TABLE).build();
658 rules.add(rule);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800659 /*
660 * TMAC rules for IPv6 packets
661 */
662 selector = DefaultTrafficSelector.builder();
663 treatment = DefaultTrafficTreatment.builder();
664 selector.matchInPort(pnum);
665 selector.extension(ofdpaMatchVlanVid, deviceId);
666 selector.matchEthType(Ethernet.TYPE_IPV6);
667 selector.matchEthDst(ethCriterion.mac());
668 treatment.transition(UNICAST_ROUTING_TABLE);
669 rule = DefaultFlowRule.builder()
670 .forDevice(deviceId)
671 .withSelector(selector.build())
672 .withTreatment(treatment.build())
673 .withPriority(DEFAULT_PRIORITY)
674 .fromApp(applicationId)
675 .makePermanent()
676 .forTable(TMAC_TABLE).build();
677 rules.add(rule);
Saurav Das822c4e22015-10-23 10:51:11 -0700678 }
679 return rules;
680 }
681
Charles Chan5270ed02016-01-30 23:22:37 -0800682 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
683 ApplicationId applicationId) {
684 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
685 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
686 selector.matchEthType(Ethernet.TYPE_IPV4);
687 selector.matchEthDst(ethCriterion.mac());
688 treatment.transition(UNICAST_ROUTING_TABLE);
689 FlowRule rule = DefaultFlowRule.builder()
690 .forDevice(deviceId)
691 .withSelector(selector.build())
692 .withTreatment(treatment.build())
693 .withPriority(DEFAULT_PRIORITY)
694 .fromApp(applicationId)
695 .makePermanent()
696 .forTable(TMAC_TABLE).build();
697 return ImmutableList.<FlowRule>builder().add(rule).build();
698 }
699
Charles Chan5b9df8d2016-03-28 22:21:40 -0700700 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
701 ApplicationId applicationId) {
702 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
703 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
704 selector.matchEthType(Ethernet.TYPE_IPV4);
705 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
706 treatment.transition(MULTICAST_ROUTING_TABLE);
707 FlowRule rule = DefaultFlowRule.builder()
708 .forDevice(deviceId)
709 .withSelector(selector.build())
710 .withTreatment(treatment.build())
711 .withPriority(DEFAULT_PRIORITY)
712 .fromApp(applicationId)
713 .makePermanent()
714 .forTable(TMAC_TABLE).build();
715 return ImmutableList.<FlowRule>builder().add(rule).build();
716 }
717
Saurav Das822c4e22015-10-23 10:51:11 -0700718 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
719 switch (fwd.flag()) {
720 case SPECIFIC:
721 return processSpecific(fwd);
722 case VERSATILE:
723 return processVersatile(fwd);
724 default:
725 fail(fwd, ObjectiveError.UNKNOWN);
726 log.warn("Unknown forwarding flag {}", fwd.flag());
727 }
728 return Collections.emptySet();
729 }
730
Pier Ventree0ae7a32016-11-23 09:57:42 -0800731
732
Saurav Das822c4e22015-10-23 10:51:11 -0700733 /**
734 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
735 * ACL table.
736 * @param fwd the forwarding objective of type 'versatile'
737 * @return a collection of flow rules to be sent to the switch. An empty
738 * collection may be returned if there is a problem in processing
739 * the flow rule
740 */
Saurav Das52025962016-01-28 22:30:01 -0800741 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800742 log.info("Processing versatile forwarding objective:{} in dev:{}",
743 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700744
745 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800746 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700747 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800748 log.error("Versatile forwarding objective:{} must include ethType",
749 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700750 fail(fwd, ObjectiveError.BADPARAMS);
751 return Collections.emptySet();
752 }
753 if (fwd.nextId() == null && fwd.treatment() == null) {
754 log.error("Forwarding objective {} from {} must contain "
755 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800756 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700757 return Collections.emptySet();
758 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800759
Saurav Das77b5e902016-01-27 17:01:59 -0800760 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
761 fwd.selector().criteria().forEach(criterion -> {
762 if (criterion instanceof VlanIdCriterion) {
763 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
764 // ensure that match does not include vlan = NONE as OF-DPA does not
765 // match untagged packets this way in the ACL table.
766 if (vlanId.equals(VlanId.NONE)) {
767 return;
768 }
769 OfdpaMatchVlanVid ofdpaMatchVlanVid =
770 new OfdpaMatchVlanVid(vlanId);
771 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800772 } else if (criterion instanceof Icmpv6TypeCriterion ||
773 criterion instanceof Icmpv6CodeCriterion) {
774 /*
775 * We silenty discard these criterions, our current
776 * OFDPA platform does not support these matches on
777 * the ACL table.
778 */
779 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800780 } else {
781 sbuilder.add(criterion);
782 }
783 });
784
Saurav Das822c4e22015-10-23 10:51:11 -0700785 // XXX driver does not currently do type checking as per Tables 65-67 in
786 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800787 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
788 if (fwd.treatment() != null) {
789 for (Instruction ins : fwd.treatment().allInstructions()) {
790 if (ins instanceof OutputInstruction) {
791 OutputInstruction o = (OutputInstruction) ins;
792 if (o.port() == PortNumber.CONTROLLER) {
793 ttBuilder.add(o);
794 } else {
795 log.warn("Only allowed treatments in versatile forwarding "
796 + "objectives are punts to the controller");
797 }
798 } else {
799 log.warn("Cannot process instruction in versatile fwd {}", ins);
800 }
Saurav Das822c4e22015-10-23 10:51:11 -0700801 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800802 if (fwd.treatment().clearedDeferred()) {
803 ttBuilder.wipeDeferred();
804 }
Saurav Das822c4e22015-10-23 10:51:11 -0700805 }
Saurav Das822c4e22015-10-23 10:51:11 -0700806 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800807 // overide case
808 NextGroup next = getGroupForNextObjective(fwd.nextId());
809 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
810 // we only need the top level group's key to point the flow to it
811 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
812 if (group == null) {
813 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
814 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
815 fail(fwd, ObjectiveError.GROUPMISSING);
816 return Collections.emptySet();
817 }
818 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700819 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800820
821 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
822 .fromApp(fwd.appId())
823 .withPriority(fwd.priority())
824 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800825 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800826 .withTreatment(ttBuilder.build())
827 .makePermanent()
828 .forTable(ACL_TABLE);
829 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700830 }
831
832 /**
833 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800834 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700835 *
836 * @param fwd the forwarding objective of type 'specific'
837 * @return a collection of flow rules. Typically there will be only one
838 * for this type of forwarding objective. An empty set may be
839 * returned if there is an issue in processing the objective.
840 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800841 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700842 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800843 fwd.id(), deviceId, fwd.nextId());
844 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
845 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
846
847 if (isEthTypeObj) {
848 return processEthTypeSpecific(fwd);
849 } else if (isEthDstObj) {
850 return processEthDstSpecific(fwd);
851 } else {
852 log.warn("processSpecific: Unsupported forwarding objective "
853 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700854 fail(fwd, ObjectiveError.UNSUPPORTED);
855 return Collections.emptySet();
856 }
Saurav Das4ce45962015-11-24 23:21:05 -0800857 }
858
Saurav Das4ce45962015-11-24 23:21:05 -0800859 /**
860 * Handles forwarding rules to the IP and MPLS tables.
861 *
862 * @param fwd the forwarding objective
863 * @return A collection of flow rules, or an empty set
864 */
865 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700866 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700867 }
868
869 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800870 * Helper method to build Ipv6 selector using the selector provided by
871 * a forwarding objective.
872 *
873 * @param builderToUpdate the builder to update
874 * @param fwd the selector to read
875 * @return 0 if the update ends correctly. -1 if the matches
876 * are not yet supported
877 */
878 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
879 ForwardingObjective fwd) {
880
881 TrafficSelector selector = fwd.selector();
882
883 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
884 if (ipv6Dst.isMulticast()) {
885 log.warn("IPv6 Multicast is currently not supported");
886 fail(fwd, ObjectiveError.BADPARAMS);
887 return -1;
888 } else {
889 if (ipv6Dst.prefixLength() == 0) {
890 log.warn("Default ipv6 route is currently not supported");
891 fail(fwd, ObjectiveError.BADPARAMS);
892 return -1;
893 } else {
894 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
895 }
896 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
897 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
898 }
899 return 0;
900 }
901
902 /**
Charles Chanf9e98652016-09-07 16:54:23 -0700903 * Internal implementation of processEthTypeSpecific.
904 * <p>
905 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
906 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
907 * The allowDefaultRoute must be set to false for OFDPA i12.
908 * </p>
909 *
910 * @param fwd the forwarding objective
911 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -0800912 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -0700913 * @return A collection of flow rules, or an empty set
914 */
915 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -0700916 boolean allowDefaultRoute,
917 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -0800918 TrafficSelector selector = fwd.selector();
919 EthTypeCriterion ethType =
920 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800921 boolean defaultRule = false;
922 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700923 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800924 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800925 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800926 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800927 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800928
Saurav Das8a0732e2015-11-20 15:27:53 -0800929 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800930 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700931 if (ipv4Dst.isMulticast()) {
932 if (ipv4Dst.prefixLength() != 32) {
933 log.warn("Multicast specific forwarding objective can only be /32");
934 fail(fwd, ObjectiveError.BADPARAMS);
935 return ImmutableSet.of();
936 }
937 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
938 if (assignedVlan == null) {
939 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
940 fail(fwd, ObjectiveError.BADPARAMS);
941 return ImmutableSet.of();
942 }
943 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
944 filteredSelector.extension(ofdpaMatchVlanVid, deviceId);
945 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
946 forTableId = MULTICAST_ROUTING_TABLE;
947 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
948 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800949 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700950 if (ipv4Dst.prefixLength() == 0) {
951 if (allowDefaultRoute) {
952 // The entire IPV4_DST field is wildcarded intentionally
953 filteredSelector.matchEthType(Ethernet.TYPE_IPV4);
954 } else {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800955 // NOTE: The switch does not support matching 0.0.0.0/0
956 // Split it into 0.0.0.0/1 and 128.0.0.0/1
Charles Chanf9e98652016-09-07 16:54:23 -0700957 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
958 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
959 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
960 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
961 defaultRule = true;
962 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700963 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700964 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700965 }
966 forTableId = UNICAST_ROUTING_TABLE;
967 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
968 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800969 }
Charles Chan14967c22015-12-07 11:11:50 -0800970
971 if (fwd.treatment() != null) {
972 for (Instruction instr : fwd.treatment().allInstructions()) {
973 if (instr instanceof L3ModificationInstruction &&
974 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -0800975 // XXX decrementing IP ttl is done automatically for routing, this
976 // action is ignored or rejected in ofdpa as it is not fully implemented
977 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -0800978 }
979 }
980 }
981
Pier Ventree0ae7a32016-11-23 09:57:42 -0800982 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
983 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
984 return Collections.emptyList();
985 }
986 forTableId = UNICAST_ROUTING_TABLE;
987 if (fwd.treatment() != null) {
988 for (Instruction instr : fwd.treatment().allInstructions()) {
989 if (instr instanceof L3ModificationInstruction &&
990 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
991 // XXX decrementing IP ttl is done automatically for routing, this
992 // action is ignored or rejected in ofdpa as it is not fully implemented
993 //tb.deferred().add(instr);
994 }
995 }
996 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800997 } else {
998 filteredSelector
999 .matchEthType(Ethernet.MPLS_UNICAST)
1000 .matchMplsLabel(((MplsCriterion)
1001 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1002 MplsBosCriterion bos = (MplsBosCriterion) selector
1003 .getCriterion(Criterion.Type.MPLS_BOS);
1004 if (bos != null) {
1005 filteredSelector.matchMplsBos(bos.mplsBos());
1006 }
1007 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001008 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1009 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001010
Charles Chan14967c22015-12-07 11:11:50 -08001011 if (fwd.treatment() != null) {
1012 for (Instruction instr : fwd.treatment().allInstructions()) {
1013 if (instr instanceof L2ModificationInstruction &&
1014 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001015 popMpls = true;
Saurav Das9c705342017-01-06 11:36:01 -08001016 // OF-DPA does not pop in MPLS table. Instead it requires
1017 // setting the MPLS_TYPE so pop can happen down the pipeline
Charles Chan14967c22015-12-07 11:11:50 -08001018 }
1019 if (instr instanceof L3ModificationInstruction &&
1020 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1021 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1022 tb.immediate().decMplsTtl();
1023 }
1024 if (instr instanceof L3ModificationInstruction &&
1025 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1026 tb.immediate().add(instr);
1027 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001028 }
1029 }
1030 }
Saurav Das822c4e22015-10-23 10:51:11 -07001031
1032 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001033 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1034 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001035 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1036 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001037 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1038 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1039 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001040 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001041 return Collections.emptySet();
1042 }
1043
Saurav Das423fe2b2015-12-04 10:52:59 -08001044 NextGroup next = getGroupForNextObjective(fwd.nextId());
1045 if (next != null) {
1046 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1047 // we only need the top level group's key to point the flow to it
1048 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1049 if (group == null) {
1050 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1051 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1052 fail(fwd, ObjectiveError.GROUPMISSING);
1053 return Collections.emptySet();
1054 }
1055 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001056 // check if group is empty
1057 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1058 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1059 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1060 emptyGroup = true;
1061 }
Saurav Das25190812016-05-27 13:54:07 -07001062 } else {
1063 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1064 fwd.nextId(), deviceId, fwd.id());
1065 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1066 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001067 }
Saurav Das822c4e22015-10-23 10:51:11 -07001068 }
Charles Chancad338a2016-09-16 18:03:11 -07001069
1070 if (forTableId == MPLS_TABLE_1) {
1071 if (mplsNextTable == MPLS_L3_TYPE) {
1072 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001073 // set mpls type as apply_action
1074 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001075 }
1076 tb.transition(mplsNextTable);
1077 } else {
1078 tb.transition(ACL_TABLE);
1079 }
1080
Saurav Das822c4e22015-10-23 10:51:11 -07001081 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1082 .fromApp(fwd.appId())
1083 .withPriority(fwd.priority())
1084 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001085 .withSelector(filteredSelector.build())
1086 .withTreatment(tb.build())
1087 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001088
1089 if (fwd.permanent()) {
1090 ruleBuilder.makePermanent();
1091 } else {
1092 ruleBuilder.makeTemporary(fwd.timeout());
1093 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001094 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1095 flowRuleCollection.add(ruleBuilder.build());
1096 if (defaultRule) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001097 flowRuleCollection.add(
1098 defaultRoute(fwd, complementarySelector, forTableId, tb)
1099 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001100 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1101 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001102 // XXX retrying flows may be necessary due to bug CORD-554
1103 if (emptyGroup) {
1104 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1105 RETRY_MS, TimeUnit.MILLISECONDS);
1106 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001107 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001108 }
1109
Pier Ventree0ae7a32016-11-23 09:57:42 -08001110 protected FlowRule defaultRoute(ForwardingObjective fwd,
1111 TrafficSelector.Builder complementarySelector,
1112 int forTableId,
1113 TrafficTreatment.Builder tb) {
1114 FlowRule.Builder rule = DefaultFlowRule.builder()
1115 .fromApp(fwd.appId())
1116 .withPriority(fwd.priority())
1117 .forDevice(deviceId)
1118 .withSelector(complementarySelector.build())
1119 .withTreatment(tb.build())
1120 .forTable(forTableId);
1121 if (fwd.permanent()) {
1122 rule.makePermanent();
1123 } else {
1124 rule.makeTemporary(fwd.timeout());
1125 }
1126 return rule.build();
1127 }
1128
Saurav Das4ce45962015-11-24 23:21:05 -08001129 /**
1130 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1131 * allowed in the bridging table - instead we use L2 Interface group or
1132 * L2 flood group
1133 *
1134 * @param fwd the forwarding objective
1135 * @return A collection of flow rules, or an empty set
1136 */
1137 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1138 List<FlowRule> rules = new ArrayList<>();
1139
1140 // Build filtered selector
1141 TrafficSelector selector = fwd.selector();
1142 EthCriterion ethCriterion = (EthCriterion) selector
1143 .getCriterion(Criterion.Type.ETH_DST);
1144 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1145 .getCriterion(Criterion.Type.VLAN_VID);
1146
1147 if (vlanIdCriterion == null) {
1148 log.warn("Forwarding objective for bridging requires vlan. Not "
1149 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1150 fail(fwd, ObjectiveError.BADPARAMS);
1151 return Collections.emptySet();
1152 }
1153
1154 TrafficSelector.Builder filteredSelectorBuilder =
1155 DefaultTrafficSelector.builder();
1156 // Do not match MacAddress for subnet broadcast entry
1157 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
1158 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1159 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1160 fwd.id(), fwd.nextId(), deviceId);
1161 } else {
1162 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1163 + "in dev:{} for vlan:{}",
1164 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1165 }
Charles Chan14967c22015-12-07 11:11:50 -08001166 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1167 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -08001168 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1169
1170 if (fwd.treatment() != null) {
1171 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1172 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1173 }
1174
1175 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1176 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001177 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001178 if (next != null) {
1179 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1180 // we only need the top level group's key to point the flow to it
1181 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1182 if (group != null) {
1183 treatmentBuilder.deferred().group(group.id());
1184 } else {
1185 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1186 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1187 fail(fwd, ObjectiveError.GROUPMISSING);
1188 return Collections.emptySet();
1189 }
1190 }
1191 }
1192 treatmentBuilder.immediate().transition(ACL_TABLE);
1193 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1194
1195 // Build bridging table entries
1196 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1197 flowRuleBuilder.fromApp(fwd.appId())
1198 .withPriority(fwd.priority())
1199 .forDevice(deviceId)
1200 .withSelector(filteredSelector)
1201 .withTreatment(filteredTreatment)
1202 .forTable(BRIDGING_TABLE);
1203 if (fwd.permanent()) {
1204 flowRuleBuilder.makePermanent();
1205 } else {
1206 flowRuleBuilder.makeTemporary(fwd.timeout());
1207 }
1208 rules.add(flowRuleBuilder.build());
1209 return rules;
1210 }
1211
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001212 //////////////////////////////////////
1213 // Helper Methods and Classes
1214 //////////////////////////////////////
1215
1216 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1217 TrafficSelector selector = fwd.selector();
1218 EthTypeCriterion ethType = (EthTypeCriterion) selector
1219 .getCriterion(Criterion.Type.ETH_TYPE);
1220 return !((ethType == null) ||
1221 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001222 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1223 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001224 }
1225
1226 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1227 TrafficSelector selector = fwd.selector();
1228 EthCriterion ethDst = (EthCriterion) selector
1229 .getCriterion(Criterion.Type.ETH_DST);
1230 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1231 .getCriterion(Criterion.Type.VLAN_VID);
1232 return !(ethDst == null && vlanId == null);
1233 }
1234
Saurav Das423fe2b2015-12-04 10:52:59 -08001235 protected NextGroup getGroupForNextObjective(Integer nextId) {
1236 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1237 if (next != null) {
1238 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1239 if (gkeys != null && !gkeys.isEmpty()) {
1240 return next;
1241 } else {
1242 log.warn("Empty next group found in FlowObjective store for "
1243 + "next-id:{} in dev:{}", nextId, deviceId);
1244 }
1245 } else {
1246 log.warn("next-id {} not found in Flow objective store for dev:{}",
1247 nextId, deviceId);
1248 }
1249 return null;
1250 }
1251
Charles Chan188ebf52015-12-23 00:15:11 -08001252 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001253 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001254 }
1255
Charles Chan188ebf52015-12-23 00:15:11 -08001256 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001257 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001258 }
Saurav Das24431192016-03-07 19:13:00 -08001259
Saurav Das24431192016-03-07 19:13:00 -08001260 @Override
1261 public List<String> getNextMappings(NextGroup nextGroup) {
1262 List<String> mappings = new ArrayList<>();
1263 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1264 for (Deque<GroupKey> gkd : gkeys) {
1265 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001266 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001267 for (GroupKey gk : gkd) {
1268 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001269 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001270 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001271 continue;
1272 }
1273 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1274 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001275 lastGroup = g;
1276 }
1277 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001278 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001279 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001280 lastGroupIns = lastGroup.buckets().buckets().get(0)
1281 .treatment().allInstructions();
1282 }
1283 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001284 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001285 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001286 }
1287 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001288 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001289 }
1290 return mappings;
1291 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001292
1293 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001294 if (selector == null) {
1295 return null;
1296 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001297 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1298 return (criterion == null)
1299 ? null : ((VlanIdCriterion) criterion).vlanId();
1300 }
1301
1302 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001303 if (selector == null) {
1304 return null;
1305 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001306 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1307 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1308 }
Charles Chand55e84d2016-03-30 17:54:24 -07001309
1310 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001311 if (treatment == null) {
1312 return null;
1313 }
Charles Chand55e84d2016-03-30 17:54:24 -07001314 for (Instruction i : treatment.allInstructions()) {
1315 if (i instanceof ModVlanIdInstruction) {
1316 return ((ModVlanIdInstruction) i).vlanId();
1317 }
1318 }
1319 return null;
1320 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001321
1322 /**
1323 * Utility class that retries sending flows a fixed number of times, even if
1324 * some of the attempts are successful. Used only for forwarding objectives.
1325 */
1326 protected final class RetryFlows implements Runnable {
1327 int attempts = MAX_RETRY_ATTEMPTS;
1328 private Collection<FlowRule> retryFlows;
1329 private ForwardingObjective fwd;
1330
1331 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1332 this.fwd = fwd;
1333 this.retryFlows = retryFlows;
1334 }
1335
1336 @Override
1337 public void run() {
1338 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1339 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1340 sendForward(fwd, retryFlows);
1341 if (--attempts > 0) {
1342 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1343 }
1344 }
1345 }
1346
Saurav Das822c4e22015-10-23 10:51:11 -07001347}