blob: e6249fd34f7ce56859c690f0bd6393c8db5541a3 [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) {
Pier Luigi0e358632017-01-31 09:35:05 -0800688 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
689
Charles Chan5270ed02016-01-30 23:22:37 -0800690 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
691 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
692 selector.matchEthType(Ethernet.TYPE_IPV4);
693 selector.matchEthDst(ethCriterion.mac());
694 treatment.transition(UNICAST_ROUTING_TABLE);
695 FlowRule rule = DefaultFlowRule.builder()
696 .forDevice(deviceId)
697 .withSelector(selector.build())
698 .withTreatment(treatment.build())
699 .withPriority(DEFAULT_PRIORITY)
700 .fromApp(applicationId)
701 .makePermanent()
702 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800703 builder.add(rule);
704
705 selector = DefaultTrafficSelector.builder();
706 treatment = DefaultTrafficTreatment.builder();
707 selector.matchEthType(Ethernet.TYPE_IPV6);
708 selector.matchEthDst(ethCriterion.mac());
709 treatment.transition(UNICAST_ROUTING_TABLE);
710 rule = DefaultFlowRule.builder()
711 .forDevice(deviceId)
712 .withSelector(selector.build())
713 .withTreatment(treatment.build())
714 .withPriority(DEFAULT_PRIORITY)
715 .fromApp(applicationId)
716 .makePermanent()
717 .forTable(TMAC_TABLE).build();
718 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800719 }
720
Charles Chan5b9df8d2016-03-28 22:21:40 -0700721 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
722 ApplicationId applicationId) {
723 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
724 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
725 selector.matchEthType(Ethernet.TYPE_IPV4);
726 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
727 treatment.transition(MULTICAST_ROUTING_TABLE);
728 FlowRule rule = DefaultFlowRule.builder()
729 .forDevice(deviceId)
730 .withSelector(selector.build())
731 .withTreatment(treatment.build())
732 .withPriority(DEFAULT_PRIORITY)
733 .fromApp(applicationId)
734 .makePermanent()
735 .forTable(TMAC_TABLE).build();
736 return ImmutableList.<FlowRule>builder().add(rule).build();
737 }
738
Saurav Das822c4e22015-10-23 10:51:11 -0700739 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
740 switch (fwd.flag()) {
741 case SPECIFIC:
742 return processSpecific(fwd);
743 case VERSATILE:
744 return processVersatile(fwd);
745 default:
746 fail(fwd, ObjectiveError.UNKNOWN);
747 log.warn("Unknown forwarding flag {}", fwd.flag());
748 }
749 return Collections.emptySet();
750 }
751
Pier Ventree0ae7a32016-11-23 09:57:42 -0800752
753
Saurav Das822c4e22015-10-23 10:51:11 -0700754 /**
755 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
756 * ACL table.
757 * @param fwd the forwarding objective of type 'versatile'
758 * @return a collection of flow rules to be sent to the switch. An empty
759 * collection may be returned if there is a problem in processing
760 * the flow rule
761 */
Saurav Das52025962016-01-28 22:30:01 -0800762 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800763 log.info("Processing versatile forwarding objective:{} in dev:{}",
764 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700765
766 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800767 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700768 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800769 log.error("Versatile forwarding objective:{} must include ethType",
770 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700771 fail(fwd, ObjectiveError.BADPARAMS);
772 return Collections.emptySet();
773 }
774 if (fwd.nextId() == null && fwd.treatment() == null) {
775 log.error("Forwarding objective {} from {} must contain "
776 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800777 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700778 return Collections.emptySet();
779 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800780
Saurav Das77b5e902016-01-27 17:01:59 -0800781 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
782 fwd.selector().criteria().forEach(criterion -> {
783 if (criterion instanceof VlanIdCriterion) {
784 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
785 // ensure that match does not include vlan = NONE as OF-DPA does not
786 // match untagged packets this way in the ACL table.
787 if (vlanId.equals(VlanId.NONE)) {
788 return;
789 }
790 OfdpaMatchVlanVid ofdpaMatchVlanVid =
791 new OfdpaMatchVlanVid(vlanId);
792 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800793 } else if (criterion instanceof Icmpv6TypeCriterion ||
794 criterion instanceof Icmpv6CodeCriterion) {
795 /*
796 * We silenty discard these criterions, our current
797 * OFDPA platform does not support these matches on
798 * the ACL table.
799 */
800 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800801 } else {
802 sbuilder.add(criterion);
803 }
804 });
805
Saurav Das822c4e22015-10-23 10:51:11 -0700806 // XXX driver does not currently do type checking as per Tables 65-67 in
807 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800808 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
809 if (fwd.treatment() != null) {
810 for (Instruction ins : fwd.treatment().allInstructions()) {
811 if (ins instanceof OutputInstruction) {
812 OutputInstruction o = (OutputInstruction) ins;
813 if (o.port() == PortNumber.CONTROLLER) {
814 ttBuilder.add(o);
815 } else {
816 log.warn("Only allowed treatments in versatile forwarding "
817 + "objectives are punts to the controller");
818 }
819 } else {
820 log.warn("Cannot process instruction in versatile fwd {}", ins);
821 }
Saurav Das822c4e22015-10-23 10:51:11 -0700822 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800823 if (fwd.treatment().clearedDeferred()) {
824 ttBuilder.wipeDeferred();
825 }
Saurav Das822c4e22015-10-23 10:51:11 -0700826 }
Saurav Das822c4e22015-10-23 10:51:11 -0700827 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800828 // overide case
829 NextGroup next = getGroupForNextObjective(fwd.nextId());
830 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
831 // we only need the top level group's key to point the flow to it
832 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
833 if (group == null) {
834 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
835 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
836 fail(fwd, ObjectiveError.GROUPMISSING);
837 return Collections.emptySet();
838 }
839 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700840 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800841
842 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
843 .fromApp(fwd.appId())
844 .withPriority(fwd.priority())
845 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800846 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800847 .withTreatment(ttBuilder.build())
848 .makePermanent()
849 .forTable(ACL_TABLE);
850 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700851 }
852
853 /**
854 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800855 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700856 *
857 * @param fwd the forwarding objective of type 'specific'
858 * @return a collection of flow rules. Typically there will be only one
859 * for this type of forwarding objective. An empty set may be
860 * returned if there is an issue in processing the objective.
861 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800862 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700863 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800864 fwd.id(), deviceId, fwd.nextId());
865 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
866 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
867
868 if (isEthTypeObj) {
869 return processEthTypeSpecific(fwd);
870 } else if (isEthDstObj) {
871 return processEthDstSpecific(fwd);
872 } else {
873 log.warn("processSpecific: Unsupported forwarding objective "
874 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700875 fail(fwd, ObjectiveError.UNSUPPORTED);
876 return Collections.emptySet();
877 }
Saurav Das4ce45962015-11-24 23:21:05 -0800878 }
879
Saurav Das4ce45962015-11-24 23:21:05 -0800880 /**
881 * Handles forwarding rules to the IP and MPLS tables.
882 *
883 * @param fwd the forwarding objective
884 * @return A collection of flow rules, or an empty set
885 */
886 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700887 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700888 }
889
890 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800891 * Helper method to build Ipv6 selector using the selector provided by
892 * a forwarding objective.
893 *
894 * @param builderToUpdate the builder to update
895 * @param fwd the selector to read
896 * @return 0 if the update ends correctly. -1 if the matches
897 * are not yet supported
898 */
899 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
900 ForwardingObjective fwd) {
901
902 TrafficSelector selector = fwd.selector();
903
904 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
905 if (ipv6Dst.isMulticast()) {
906 log.warn("IPv6 Multicast is currently not supported");
907 fail(fwd, ObjectiveError.BADPARAMS);
908 return -1;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800909 }
Pier Luigi69fd4312017-01-28 17:58:58 -0800910 if (ipv6Dst.prefixLength() != 0) {
911 builderToUpdate.matchIPv6Dst(ipv6Dst);
912 }
913 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
914 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
915 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800916 return 0;
917 }
918
919 /**
Charles Chanf9e98652016-09-07 16:54:23 -0700920 * Internal implementation of processEthTypeSpecific.
921 * <p>
922 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
923 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
924 * The allowDefaultRoute must be set to false for OFDPA i12.
925 * </p>
926 *
927 * @param fwd the forwarding objective
928 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -0800929 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -0700930 * @return A collection of flow rules, or an empty set
931 */
932 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -0700933 boolean allowDefaultRoute,
934 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -0800935 TrafficSelector selector = fwd.selector();
936 EthTypeCriterion ethType =
937 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800938 boolean defaultRule = false;
939 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700940 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800941 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800942 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800943 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800944 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800945
Saurav Das8a0732e2015-11-20 15:27:53 -0800946 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800947 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700948 if (ipv4Dst.isMulticast()) {
949 if (ipv4Dst.prefixLength() != 32) {
950 log.warn("Multicast specific forwarding objective can only be /32");
951 fail(fwd, ObjectiveError.BADPARAMS);
952 return ImmutableSet.of();
953 }
954 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
955 if (assignedVlan == null) {
956 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
957 fail(fwd, ObjectiveError.BADPARAMS);
958 return ImmutableSet.of();
959 }
960 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
961 filteredSelector.extension(ofdpaMatchVlanVid, deviceId);
962 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
963 forTableId = MULTICAST_ROUTING_TABLE;
964 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
965 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800966 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700967 if (ipv4Dst.prefixLength() == 0) {
968 if (allowDefaultRoute) {
969 // The entire IPV4_DST field is wildcarded intentionally
970 filteredSelector.matchEthType(Ethernet.TYPE_IPV4);
971 } else {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800972 // NOTE: The switch does not support matching 0.0.0.0/0
973 // Split it into 0.0.0.0/1 and 128.0.0.0/1
Charles Chanf9e98652016-09-07 16:54:23 -0700974 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
975 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
976 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
977 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
978 defaultRule = true;
979 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700980 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700981 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700982 }
983 forTableId = UNICAST_ROUTING_TABLE;
984 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
985 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800986 }
Charles Chan14967c22015-12-07 11:11:50 -0800987
988 if (fwd.treatment() != null) {
989 for (Instruction instr : fwd.treatment().allInstructions()) {
990 if (instr instanceof L3ModificationInstruction &&
991 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -0800992 // XXX decrementing IP ttl is done automatically for routing, this
993 // action is ignored or rejected in ofdpa as it is not fully implemented
994 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -0800995 }
996 }
997 }
998
Pier Ventree0ae7a32016-11-23 09:57:42 -0800999 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1000 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1001 return Collections.emptyList();
1002 }
1003 forTableId = UNICAST_ROUTING_TABLE;
1004 if (fwd.treatment() != null) {
1005 for (Instruction instr : fwd.treatment().allInstructions()) {
1006 if (instr instanceof L3ModificationInstruction &&
1007 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1008 // XXX decrementing IP ttl is done automatically for routing, this
1009 // action is ignored or rejected in ofdpa as it is not fully implemented
1010 //tb.deferred().add(instr);
1011 }
1012 }
1013 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001014 } else {
1015 filteredSelector
1016 .matchEthType(Ethernet.MPLS_UNICAST)
1017 .matchMplsLabel(((MplsCriterion)
1018 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1019 MplsBosCriterion bos = (MplsBosCriterion) selector
1020 .getCriterion(Criterion.Type.MPLS_BOS);
1021 if (bos != null) {
1022 filteredSelector.matchMplsBos(bos.mplsBos());
1023 }
1024 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001025 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1026 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001027
Charles Chan14967c22015-12-07 11:11:50 -08001028 if (fwd.treatment() != null) {
1029 for (Instruction instr : fwd.treatment().allInstructions()) {
1030 if (instr instanceof L2ModificationInstruction &&
1031 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001032 popMpls = true;
Saurav Das9c705342017-01-06 11:36:01 -08001033 // OF-DPA does not pop in MPLS table. Instead it requires
1034 // setting the MPLS_TYPE so pop can happen down the pipeline
Charles Chan14967c22015-12-07 11:11:50 -08001035 }
1036 if (instr instanceof L3ModificationInstruction &&
1037 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1038 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1039 tb.immediate().decMplsTtl();
1040 }
1041 if (instr instanceof L3ModificationInstruction &&
1042 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1043 tb.immediate().add(instr);
1044 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001045 }
1046 }
1047 }
Saurav Das822c4e22015-10-23 10:51:11 -07001048
1049 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001050 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1051 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001052 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1053 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001054 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1055 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1056 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001057 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001058 return Collections.emptySet();
1059 }
1060
Saurav Das423fe2b2015-12-04 10:52:59 -08001061 NextGroup next = getGroupForNextObjective(fwd.nextId());
1062 if (next != null) {
1063 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1064 // we only need the top level group's key to point the flow to it
1065 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1066 if (group == null) {
1067 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1068 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1069 fail(fwd, ObjectiveError.GROUPMISSING);
1070 return Collections.emptySet();
1071 }
1072 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001073 // check if group is empty
1074 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1075 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1076 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1077 emptyGroup = true;
1078 }
Saurav Das25190812016-05-27 13:54:07 -07001079 } else {
1080 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1081 fwd.nextId(), deviceId, fwd.id());
1082 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1083 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001084 }
Saurav Das822c4e22015-10-23 10:51:11 -07001085 }
Charles Chancad338a2016-09-16 18:03:11 -07001086
1087 if (forTableId == MPLS_TABLE_1) {
1088 if (mplsNextTable == MPLS_L3_TYPE) {
1089 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001090 // set mpls type as apply_action
1091 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001092 }
1093 tb.transition(mplsNextTable);
1094 } else {
1095 tb.transition(ACL_TABLE);
1096 }
1097
Saurav Das822c4e22015-10-23 10:51:11 -07001098 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1099 .fromApp(fwd.appId())
1100 .withPriority(fwd.priority())
1101 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001102 .withSelector(filteredSelector.build())
1103 .withTreatment(tb.build())
1104 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001105
1106 if (fwd.permanent()) {
1107 ruleBuilder.makePermanent();
1108 } else {
1109 ruleBuilder.makeTemporary(fwd.timeout());
1110 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001111 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1112 flowRuleCollection.add(ruleBuilder.build());
1113 if (defaultRule) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001114 flowRuleCollection.add(
1115 defaultRoute(fwd, complementarySelector, forTableId, tb)
1116 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001117 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1118 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001119 // XXX retrying flows may be necessary due to bug CORD-554
1120 if (emptyGroup) {
1121 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1122 RETRY_MS, TimeUnit.MILLISECONDS);
1123 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001124 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001125 }
1126
Pier Ventree0ae7a32016-11-23 09:57:42 -08001127 protected FlowRule defaultRoute(ForwardingObjective fwd,
1128 TrafficSelector.Builder complementarySelector,
1129 int forTableId,
1130 TrafficTreatment.Builder tb) {
1131 FlowRule.Builder rule = DefaultFlowRule.builder()
1132 .fromApp(fwd.appId())
1133 .withPriority(fwd.priority())
1134 .forDevice(deviceId)
1135 .withSelector(complementarySelector.build())
1136 .withTreatment(tb.build())
1137 .forTable(forTableId);
1138 if (fwd.permanent()) {
1139 rule.makePermanent();
1140 } else {
1141 rule.makeTemporary(fwd.timeout());
1142 }
1143 return rule.build();
1144 }
1145
Saurav Das4ce45962015-11-24 23:21:05 -08001146 /**
1147 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1148 * allowed in the bridging table - instead we use L2 Interface group or
1149 * L2 flood group
1150 *
1151 * @param fwd the forwarding objective
1152 * @return A collection of flow rules, or an empty set
1153 */
1154 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1155 List<FlowRule> rules = new ArrayList<>();
1156
1157 // Build filtered selector
1158 TrafficSelector selector = fwd.selector();
1159 EthCriterion ethCriterion = (EthCriterion) selector
1160 .getCriterion(Criterion.Type.ETH_DST);
1161 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1162 .getCriterion(Criterion.Type.VLAN_VID);
1163
1164 if (vlanIdCriterion == null) {
1165 log.warn("Forwarding objective for bridging requires vlan. Not "
1166 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1167 fail(fwd, ObjectiveError.BADPARAMS);
1168 return Collections.emptySet();
1169 }
1170
1171 TrafficSelector.Builder filteredSelectorBuilder =
1172 DefaultTrafficSelector.builder();
1173 // Do not match MacAddress for subnet broadcast entry
1174 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
1175 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1176 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1177 fwd.id(), fwd.nextId(), deviceId);
1178 } else {
1179 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1180 + "in dev:{} for vlan:{}",
1181 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1182 }
Charles Chan14967c22015-12-07 11:11:50 -08001183 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1184 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -08001185 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1186
1187 if (fwd.treatment() != null) {
1188 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1189 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1190 }
1191
1192 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1193 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001194 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001195 if (next != null) {
1196 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1197 // we only need the top level group's key to point the flow to it
1198 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1199 if (group != null) {
1200 treatmentBuilder.deferred().group(group.id());
1201 } else {
1202 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1203 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1204 fail(fwd, ObjectiveError.GROUPMISSING);
1205 return Collections.emptySet();
1206 }
1207 }
1208 }
1209 treatmentBuilder.immediate().transition(ACL_TABLE);
1210 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1211
1212 // Build bridging table entries
1213 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1214 flowRuleBuilder.fromApp(fwd.appId())
1215 .withPriority(fwd.priority())
1216 .forDevice(deviceId)
1217 .withSelector(filteredSelector)
1218 .withTreatment(filteredTreatment)
1219 .forTable(BRIDGING_TABLE);
1220 if (fwd.permanent()) {
1221 flowRuleBuilder.makePermanent();
1222 } else {
1223 flowRuleBuilder.makeTemporary(fwd.timeout());
1224 }
1225 rules.add(flowRuleBuilder.build());
1226 return rules;
1227 }
1228
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001229 //////////////////////////////////////
1230 // Helper Methods and Classes
1231 //////////////////////////////////////
1232
1233 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1234 TrafficSelector selector = fwd.selector();
1235 EthTypeCriterion ethType = (EthTypeCriterion) selector
1236 .getCriterion(Criterion.Type.ETH_TYPE);
1237 return !((ethType == null) ||
1238 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001239 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1240 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001241 }
1242
1243 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1244 TrafficSelector selector = fwd.selector();
1245 EthCriterion ethDst = (EthCriterion) selector
1246 .getCriterion(Criterion.Type.ETH_DST);
1247 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1248 .getCriterion(Criterion.Type.VLAN_VID);
1249 return !(ethDst == null && vlanId == null);
1250 }
1251
Saurav Das423fe2b2015-12-04 10:52:59 -08001252 protected NextGroup getGroupForNextObjective(Integer nextId) {
1253 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1254 if (next != null) {
1255 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1256 if (gkeys != null && !gkeys.isEmpty()) {
1257 return next;
1258 } else {
1259 log.warn("Empty next group found in FlowObjective store for "
1260 + "next-id:{} in dev:{}", nextId, deviceId);
1261 }
1262 } else {
1263 log.warn("next-id {} not found in Flow objective store for dev:{}",
1264 nextId, deviceId);
1265 }
1266 return null;
1267 }
1268
Charles Chan188ebf52015-12-23 00:15:11 -08001269 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001270 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001271 }
1272
Charles Chan188ebf52015-12-23 00:15:11 -08001273 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001274 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001275 }
Saurav Das24431192016-03-07 19:13:00 -08001276
Saurav Das24431192016-03-07 19:13:00 -08001277 @Override
1278 public List<String> getNextMappings(NextGroup nextGroup) {
1279 List<String> mappings = new ArrayList<>();
1280 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1281 for (Deque<GroupKey> gkd : gkeys) {
1282 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001283 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001284 for (GroupKey gk : gkd) {
1285 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001286 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001287 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001288 continue;
1289 }
1290 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1291 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001292 lastGroup = g;
1293 }
1294 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001295 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001296 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001297 lastGroupIns = lastGroup.buckets().buckets().get(0)
1298 .treatment().allInstructions();
1299 }
1300 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001301 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001302 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001303 }
1304 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001305 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001306 }
1307 return mappings;
1308 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001309
1310 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001311 if (selector == null) {
1312 return null;
1313 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001314 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1315 return (criterion == null)
1316 ? null : ((VlanIdCriterion) criterion).vlanId();
1317 }
1318
1319 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001320 if (selector == null) {
1321 return null;
1322 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001323 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1324 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1325 }
Charles Chand55e84d2016-03-30 17:54:24 -07001326
1327 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001328 if (treatment == null) {
1329 return null;
1330 }
Charles Chand55e84d2016-03-30 17:54:24 -07001331 for (Instruction i : treatment.allInstructions()) {
1332 if (i instanceof ModVlanIdInstruction) {
1333 return ((ModVlanIdInstruction) i).vlanId();
1334 }
1335 }
1336 return null;
1337 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001338
1339 /**
1340 * Utility class that retries sending flows a fixed number of times, even if
1341 * some of the attempts are successful. Used only for forwarding objectives.
1342 */
1343 protected final class RetryFlows implements Runnable {
1344 int attempts = MAX_RETRY_ATTEMPTS;
1345 private Collection<FlowRule> retryFlows;
1346 private ForwardingObjective fwd;
1347
1348 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1349 this.fwd = fwd;
1350 this.retryFlows = retryFlows;
1351 }
1352
1353 @Override
1354 public void run() {
1355 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1356 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1357 sendForward(fwd, retryFlows);
1358 if (--attempts > 0) {
1359 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1360 }
1361 }
1362 }
1363
Saurav Das822c4e22015-10-23 10:51:11 -07001364}