blob: 03811e2f536911db39ff15bfa6b658a6f24413eb [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())) {
Charles Chanc03782d2017-01-31 13:57:55 -0800530 if (useSetVlanExtension) {
531 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
532 treatment.extension(ofdpaSetVlanVid, deviceId);
533 } else {
534 treatment.setVlanId(assignedVlan);
535 }
Charles Chand55e84d2016-03-30 17:54:24 -0700536 }
Saurav Das822c4e22015-10-23 10:51:11 -0700537 }
Saurav Das822c4e22015-10-23 10:51:11 -0700538
539 // ofdpa cannot match on ALL portnumber, so we need to use separate
540 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800541 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700542 if (portCriterion.port() == PortNumber.ALL) {
543 for (Port port : deviceService.getPorts(deviceId)) {
544 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
545 portnums.add(port.number());
546 }
547 }
548 } else {
549 portnums.add(portCriterion.port());
550 }
Saurav Das4f980082015-11-05 13:39:15 -0800551
Saurav Das822c4e22015-10-23 10:51:11 -0700552 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800553 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700554 selector.matchInPort(pnum);
555 FlowRule rule = DefaultFlowRule.builder()
556 .forDevice(deviceId)
557 .withSelector(selector.build())
558 .withTreatment(treatment.build())
559 .withPriority(DEFAULT_PRIORITY)
560 .fromApp(applicationId)
561 .makePermanent()
562 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800563
564 if (preSelector != null) {
565 preSelector.matchInPort(pnum);
566 FlowRule preRule = DefaultFlowRule.builder()
567 .forDevice(deviceId)
568 .withSelector(preSelector.build())
569 .withTreatment(preTreatment.build())
570 .withPriority(DEFAULT_PRIORITY)
571 .fromApp(applicationId)
572 .makePermanent()
573 .forTable(VLAN_TABLE).build();
574 rules.add(preRule);
575 }
576
Saurav Das822c4e22015-10-23 10:51:11 -0700577 rules.add(rule);
578 }
579 return rules;
580 }
581
582 /**
583 * Allows routed packets with correct destination MAC to be directed
584 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700585 *
586 * @param portCriterion port on device for which this filter is programmed
587 * @param ethCriterion dstMac of device for which is filter is programmed
588 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700589 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700590 * @param applicationId for application programming this filter
591 * @return list of FlowRule for port-vlan filters
592
593 */
594 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
595 EthCriterion ethCriterion,
596 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700597 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700598 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800599 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
600 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
601 return processEthDstOnlyFilter(ethCriterion, applicationId);
602 }
603
Charles Chan5b9df8d2016-03-28 22:21:40 -0700604 // Multicast MAC
605 if (ethCriterion.mask() != null) {
606 return processMcastEthDstFilter(ethCriterion, applicationId);
607 }
608
Saurav Das822c4e22015-10-23 10:51:11 -0700609 //handling untagged packets via assigned VLAN
610 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700611 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700612 }
613 // ofdpa cannot match on ALL portnumber, so we need to use separate
614 // rules for each port.
615 List<PortNumber> portnums = new ArrayList<PortNumber>();
616 if (portCriterion.port() == PortNumber.ALL) {
617 for (Port port : deviceService.getPorts(deviceId)) {
618 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
619 portnums.add(port.number());
620 }
621 }
622 } else {
623 portnums.add(portCriterion.port());
624 }
625
626 List<FlowRule> rules = new ArrayList<FlowRule>();
627 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800628 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700629 // for unicast IP packets
630 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
631 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
632 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800633 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700634 selector.matchEthType(Ethernet.TYPE_IPV4);
635 selector.matchEthDst(ethCriterion.mac());
636 treatment.transition(UNICAST_ROUTING_TABLE);
637 FlowRule rule = DefaultFlowRule.builder()
638 .forDevice(deviceId)
639 .withSelector(selector.build())
640 .withTreatment(treatment.build())
641 .withPriority(DEFAULT_PRIORITY)
642 .fromApp(applicationId)
643 .makePermanent()
644 .forTable(TMAC_TABLE).build();
645 rules.add(rule);
646 //for MPLS packets
647 selector = DefaultTrafficSelector.builder();
648 treatment = DefaultTrafficTreatment.builder();
649 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800650 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700651 selector.matchEthType(Ethernet.MPLS_UNICAST);
652 selector.matchEthDst(ethCriterion.mac());
653 treatment.transition(MPLS_TABLE_0);
654 rule = DefaultFlowRule.builder()
655 .forDevice(deviceId)
656 .withSelector(selector.build())
657 .withTreatment(treatment.build())
658 .withPriority(DEFAULT_PRIORITY)
659 .fromApp(applicationId)
660 .makePermanent()
661 .forTable(TMAC_TABLE).build();
662 rules.add(rule);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800663 /*
664 * TMAC rules for IPv6 packets
665 */
666 selector = DefaultTrafficSelector.builder();
667 treatment = DefaultTrafficTreatment.builder();
668 selector.matchInPort(pnum);
669 selector.extension(ofdpaMatchVlanVid, deviceId);
670 selector.matchEthType(Ethernet.TYPE_IPV6);
671 selector.matchEthDst(ethCriterion.mac());
672 treatment.transition(UNICAST_ROUTING_TABLE);
673 rule = DefaultFlowRule.builder()
674 .forDevice(deviceId)
675 .withSelector(selector.build())
676 .withTreatment(treatment.build())
677 .withPriority(DEFAULT_PRIORITY)
678 .fromApp(applicationId)
679 .makePermanent()
680 .forTable(TMAC_TABLE).build();
681 rules.add(rule);
Saurav Das822c4e22015-10-23 10:51:11 -0700682 }
683 return rules;
684 }
685
Charles Chan5270ed02016-01-30 23:22:37 -0800686 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
687 ApplicationId applicationId) {
688 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
689 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
690 selector.matchEthType(Ethernet.TYPE_IPV4);
691 selector.matchEthDst(ethCriterion.mac());
692 treatment.transition(UNICAST_ROUTING_TABLE);
693 FlowRule rule = DefaultFlowRule.builder()
694 .forDevice(deviceId)
695 .withSelector(selector.build())
696 .withTreatment(treatment.build())
697 .withPriority(DEFAULT_PRIORITY)
698 .fromApp(applicationId)
699 .makePermanent()
700 .forTable(TMAC_TABLE).build();
701 return ImmutableList.<FlowRule>builder().add(rule).build();
702 }
703
Charles Chan5b9df8d2016-03-28 22:21:40 -0700704 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
705 ApplicationId applicationId) {
706 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
707 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
708 selector.matchEthType(Ethernet.TYPE_IPV4);
709 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
710 treatment.transition(MULTICAST_ROUTING_TABLE);
711 FlowRule rule = DefaultFlowRule.builder()
712 .forDevice(deviceId)
713 .withSelector(selector.build())
714 .withTreatment(treatment.build())
715 .withPriority(DEFAULT_PRIORITY)
716 .fromApp(applicationId)
717 .makePermanent()
718 .forTable(TMAC_TABLE).build();
719 return ImmutableList.<FlowRule>builder().add(rule).build();
720 }
721
Saurav Das822c4e22015-10-23 10:51:11 -0700722 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
723 switch (fwd.flag()) {
724 case SPECIFIC:
725 return processSpecific(fwd);
726 case VERSATILE:
727 return processVersatile(fwd);
728 default:
729 fail(fwd, ObjectiveError.UNKNOWN);
730 log.warn("Unknown forwarding flag {}", fwd.flag());
731 }
732 return Collections.emptySet();
733 }
734
Pier Ventree0ae7a32016-11-23 09:57:42 -0800735
736
Saurav Das822c4e22015-10-23 10:51:11 -0700737 /**
738 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
739 * ACL table.
740 * @param fwd the forwarding objective of type 'versatile'
741 * @return a collection of flow rules to be sent to the switch. An empty
742 * collection may be returned if there is a problem in processing
743 * the flow rule
744 */
Saurav Das52025962016-01-28 22:30:01 -0800745 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800746 log.info("Processing versatile forwarding objective:{} in dev:{}",
747 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700748
749 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800750 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700751 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800752 log.error("Versatile forwarding objective:{} must include ethType",
753 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700754 fail(fwd, ObjectiveError.BADPARAMS);
755 return Collections.emptySet();
756 }
757 if (fwd.nextId() == null && fwd.treatment() == null) {
758 log.error("Forwarding objective {} from {} must contain "
759 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800760 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700761 return Collections.emptySet();
762 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800763
Saurav Das77b5e902016-01-27 17:01:59 -0800764 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
765 fwd.selector().criteria().forEach(criterion -> {
766 if (criterion instanceof VlanIdCriterion) {
767 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
768 // ensure that match does not include vlan = NONE as OF-DPA does not
769 // match untagged packets this way in the ACL table.
770 if (vlanId.equals(VlanId.NONE)) {
771 return;
772 }
773 OfdpaMatchVlanVid ofdpaMatchVlanVid =
774 new OfdpaMatchVlanVid(vlanId);
775 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800776 } else if (criterion instanceof Icmpv6TypeCriterion ||
777 criterion instanceof Icmpv6CodeCriterion) {
778 /*
779 * We silenty discard these criterions, our current
780 * OFDPA platform does not support these matches on
781 * the ACL table.
782 */
783 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800784 } else {
785 sbuilder.add(criterion);
786 }
787 });
788
Saurav Das822c4e22015-10-23 10:51:11 -0700789 // XXX driver does not currently do type checking as per Tables 65-67 in
790 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800791 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
792 if (fwd.treatment() != null) {
793 for (Instruction ins : fwd.treatment().allInstructions()) {
794 if (ins instanceof OutputInstruction) {
795 OutputInstruction o = (OutputInstruction) ins;
796 if (o.port() == PortNumber.CONTROLLER) {
797 ttBuilder.add(o);
798 } else {
799 log.warn("Only allowed treatments in versatile forwarding "
800 + "objectives are punts to the controller");
801 }
802 } else {
803 log.warn("Cannot process instruction in versatile fwd {}", ins);
804 }
Saurav Das822c4e22015-10-23 10:51:11 -0700805 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800806 if (fwd.treatment().clearedDeferred()) {
807 ttBuilder.wipeDeferred();
808 }
Saurav Das822c4e22015-10-23 10:51:11 -0700809 }
Saurav Das822c4e22015-10-23 10:51:11 -0700810 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800811 // overide case
812 NextGroup next = getGroupForNextObjective(fwd.nextId());
813 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
814 // we only need the top level group's key to point the flow to it
815 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
816 if (group == null) {
817 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
818 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
819 fail(fwd, ObjectiveError.GROUPMISSING);
820 return Collections.emptySet();
821 }
822 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700823 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800824
825 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
826 .fromApp(fwd.appId())
827 .withPriority(fwd.priority())
828 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800829 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800830 .withTreatment(ttBuilder.build())
831 .makePermanent()
832 .forTable(ACL_TABLE);
833 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700834 }
835
836 /**
837 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800838 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700839 *
840 * @param fwd the forwarding objective of type 'specific'
841 * @return a collection of flow rules. Typically there will be only one
842 * for this type of forwarding objective. An empty set may be
843 * returned if there is an issue in processing the objective.
844 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800845 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700846 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800847 fwd.id(), deviceId, fwd.nextId());
848 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
849 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
850
851 if (isEthTypeObj) {
852 return processEthTypeSpecific(fwd);
853 } else if (isEthDstObj) {
854 return processEthDstSpecific(fwd);
855 } else {
856 log.warn("processSpecific: Unsupported forwarding objective "
857 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700858 fail(fwd, ObjectiveError.UNSUPPORTED);
859 return Collections.emptySet();
860 }
Saurav Das4ce45962015-11-24 23:21:05 -0800861 }
862
Saurav Das4ce45962015-11-24 23:21:05 -0800863 /**
864 * Handles forwarding rules to the IP and MPLS tables.
865 *
866 * @param fwd the forwarding objective
867 * @return A collection of flow rules, or an empty set
868 */
869 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700870 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700871 }
872
873 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800874 * Helper method to build Ipv6 selector using the selector provided by
875 * a forwarding objective.
876 *
877 * @param builderToUpdate the builder to update
878 * @param fwd the selector to read
879 * @return 0 if the update ends correctly. -1 if the matches
880 * are not yet supported
881 */
882 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
883 ForwardingObjective fwd) {
884
885 TrafficSelector selector = fwd.selector();
886
887 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
888 if (ipv6Dst.isMulticast()) {
889 log.warn("IPv6 Multicast is currently not supported");
890 fail(fwd, ObjectiveError.BADPARAMS);
891 return -1;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800892 }
Pier Luigi69fd4312017-01-28 17:58:58 -0800893 if (ipv6Dst.prefixLength() != 0) {
894 builderToUpdate.matchIPv6Dst(ipv6Dst);
895 }
896 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
897 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
898 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800899 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}