blob: 4600eb8b9819891e8929750f9ce4ec92454515d6 [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
Saurav Das1ce0a7b2016-10-21 14:06:29 -070018import static java.util.concurrent.Executors.newScheduledThreadPool;
19import static org.onlab.util.Tools.groupedThreads;
Saurav Das822c4e22015-10-23 10:51:11 -070020import static org.slf4j.LoggerFactory.getLogger;
21
Saurav Das8a0732e2015-11-20 15:27:53 -080022import java.util.ArrayDeque;
Saurav Das822c4e22015-10-23 10:51:11 -070023import java.util.ArrayList;
24import java.util.Collection;
25import java.util.Collections;
Saurav Das8a0732e2015-11-20 15:27:53 -080026import java.util.Deque;
Saurav Das822c4e22015-10-23 10:51:11 -070027import java.util.List;
Sho SHIMIZU45906042016-01-13 23:05:54 -080028import java.util.Objects;
Saurav Das822c4e22015-10-23 10:51:11 -070029import java.util.Set;
30import java.util.concurrent.ConcurrentHashMap;
Saurav Das1ce0a7b2016-10-21 14:06:29 -070031import java.util.concurrent.ScheduledExecutorService;
32import java.util.concurrent.TimeUnit;
Saurav Das822c4e22015-10-23 10:51:11 -070033
Charles Chan5270ed02016-01-30 23:22:37 -080034import com.google.common.collect.ImmutableList;
Charles Chan5b9df8d2016-03-28 22:21:40 -070035import com.google.common.collect.ImmutableSet;
Saurav Das822c4e22015-10-23 10:51:11 -070036import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070037import org.onlab.packet.Ethernet;
Charles Chan14967c22015-12-07 11:11:50 -080038import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070039import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070040import org.onlab.packet.VlanId;
41import org.onlab.util.KryoNamespace;
42import org.onosproject.core.ApplicationId;
43import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070044import org.onosproject.driver.extensions.Ofdpa3MplsType;
45import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080046import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
47import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070048import org.onosproject.net.DeviceId;
49import org.onosproject.net.Port;
50import org.onosproject.net.PortNumber;
51import org.onosproject.net.behaviour.NextGroup;
52import org.onosproject.net.behaviour.Pipeliner;
53import org.onosproject.net.behaviour.PipelinerContext;
54import org.onosproject.net.device.DeviceService;
55import org.onosproject.net.driver.AbstractHandlerBehaviour;
56import org.onosproject.net.flow.DefaultFlowRule;
57import org.onosproject.net.flow.DefaultTrafficSelector;
58import org.onosproject.net.flow.DefaultTrafficTreatment;
59import org.onosproject.net.flow.FlowRule;
60import org.onosproject.net.flow.FlowRuleOperations;
61import org.onosproject.net.flow.FlowRuleOperationsContext;
62import org.onosproject.net.flow.FlowRuleService;
63import org.onosproject.net.flow.TrafficSelector;
64import org.onosproject.net.flow.TrafficTreatment;
65import org.onosproject.net.flow.criteria.Criteria;
66import org.onosproject.net.flow.criteria.Criterion;
67import org.onosproject.net.flow.criteria.EthCriterion;
68import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080069import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070070import org.onosproject.net.flow.criteria.IPCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080071import org.onosproject.net.flow.criteria.MplsBosCriterion;
72import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070073import org.onosproject.net.flow.criteria.PortCriterion;
74import org.onosproject.net.flow.criteria.VlanIdCriterion;
75import org.onosproject.net.flow.instructions.Instruction;
76import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
77import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080078import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070079import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080080import org.onosproject.net.flow.instructions.L3ModificationInstruction;
81import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.net.flowobjective.FilteringObjective;
83import org.onosproject.net.flowobjective.FlowObjectiveStore;
84import org.onosproject.net.flowobjective.ForwardingObjective;
85import org.onosproject.net.flowobjective.NextObjective;
86import org.onosproject.net.flowobjective.Objective;
87import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070088import org.onosproject.net.group.DefaultGroupKey;
89import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070090import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070091import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070092import org.onosproject.store.serializers.KryoNamespaces;
93import org.slf4j.Logger;
94
Saurav Das822c4e22015-10-23 10:51:11 -070095/**
96 * Driver for Broadcom's OF-DPA v2.0 TTP.
97 *
98 */
Charles Chan361154b2016-03-24 10:23:39 -070099public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700100 protected static final int PORT_TABLE = 0;
101 protected static final int VLAN_TABLE = 10;
102 protected static final int TMAC_TABLE = 20;
103 protected static final int UNICAST_ROUTING_TABLE = 30;
104 protected static final int MULTICAST_ROUTING_TABLE = 40;
105 protected static final int MPLS_TABLE_0 = 23;
106 protected static final int MPLS_TABLE_1 = 24;
Charles Chancad338a2016-09-16 18:03:11 -0700107 protected static final int MPLS_L3_TYPE = 27;
Saurav Das822c4e22015-10-23 10:51:11 -0700108 protected static final int BRIDGING_TABLE = 50;
109 protected static final int ACL_TABLE = 60;
110 protected static final int MAC_LEARNING_TABLE = 254;
111 protected static final long OFPP_MAX = 0xffffff00L;
112
Saurav Das52025962016-01-28 22:30:01 -0800113 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800114 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700115 protected static final int LOWEST_PRIORITY = 0x0;
116
Saurav Das822c4e22015-10-23 10:51:11 -0700117 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700118 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700119 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700120 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800121 protected GroupService groupService;
122 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700123 protected DeviceId deviceId;
124 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700125 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800126 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700127 .register(KryoNamespaces.API)
128 .register(GroupKey.class)
129 .register(DefaultGroupKey.class)
Charles Chan361154b2016-03-24 10:23:39 -0700130 .register(Ofdpa2GroupHandler.OfdpaNextGroup.class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800131 .register(ArrayDeque.class)
Charles Chaneefdedf2016-05-23 16:45:45 -0700132 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700133
Charles Chan425854b2016-04-11 15:32:12 -0700134 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700135
Saurav Das52025962016-01-28 22:30:01 -0800136 protected Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800137 new ConcurrentHashMap<>());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700138 // flows installations to be retried
139 protected ScheduledExecutorService executorService
140 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
141 protected static final int MAX_RETRY_ATTEMPTS = 10;
142 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800143
Saurav Das822c4e22015-10-23 10:51:11 -0700144 @Override
145 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700146 this.deviceId = deviceId;
147
Charles Chan188ebf52015-12-23 00:15:11 -0800148 // Initialize OFDPA group handler
Charles Chan425854b2016-04-11 15:32:12 -0700149 groupHandler = new Ofdpa2GroupHandler();
150 groupHandler.init(deviceId, context);
Saurav Das822c4e22015-10-23 10:51:11 -0700151
Charles Chan425854b2016-04-11 15:32:12 -0700152 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700153 coreService = serviceDirectory.get(CoreService.class);
154 flowRuleService = serviceDirectory.get(FlowRuleService.class);
155 groupService = serviceDirectory.get(GroupService.class);
156 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700157 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700158
159 driverId = coreService.registerApplication(
Charles Chan425854b2016-04-11 15:32:12 -0700160 "org.onosproject.driver.Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700161
Saurav Das822c4e22015-10-23 10:51:11 -0700162 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700163 }
164
165 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800166 // OF-DPA does not require initializing the pipeline as it puts default
167 // rules automatically in the hardware. However emulation of OFDPA in
168 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700169 }
170
171 //////////////////////////////////////
172 // Flow Objectives
173 //////////////////////////////////////
174
175 @Override
176 public void filter(FilteringObjective filteringObjective) {
177 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
178 processFilter(filteringObjective,
179 filteringObjective.op() == Objective.Operation.ADD,
180 filteringObjective.appId());
181 } else {
182 // Note that packets that don't match the PERMIT filter are
183 // automatically denied. The DENY filter is used to deny packets
184 // that are otherwise permitted by the PERMIT filter.
185 // Use ACL table flow rules here for DENY filtering objectives
186 log.debug("filter objective other than PERMIT currently not supported");
187 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
188 }
189 }
190
191 @Override
192 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700193 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700194 if (rules == null || rules.isEmpty()) {
195 // Assumes fail message has already been generated to the objective
196 // context. Returning here prevents spurious pass message to be
197 // generated by FlowRule service for empty flowOps.
198 return;
199 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700200 sendForward(fwd, rules);
201 }
202
203 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
204 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700205 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700206 case ADD:
207 rules.stream()
208 .filter(Objects::nonNull)
209 .forEach(flowOpsBuilder::add);
210 log.info("Applying a flow rule to sw:{}", deviceId);
211 break;
212 case REMOVE:
213 rules.stream()
214 .filter(Objects::nonNull)
215 .forEach(flowOpsBuilder::remove);
216 break;
217 default:
218 fail(fwd, ObjectiveError.UNKNOWN);
219 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700220 }
221
Saurav Das822c4e22015-10-23 10:51:11 -0700222 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
223 @Override
224 public void onSuccess(FlowRuleOperations ops) {
225 pass(fwd);
226 }
227
228 @Override
229 public void onError(FlowRuleOperations ops) {
230 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
231 }
232 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700233 }
234
235 @Override
236 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800237 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
238 switch (nextObjective.op()) {
239 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800240 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800241 log.warn("Cannot add next {} that already exists in device {}",
242 nextObjective.id(), deviceId);
243 return;
244 }
245 log.debug("Processing NextObjective id{} in dev{} - add group",
246 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700247 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800248 break;
249 case ADD_TO_EXISTING:
250 if (nextGroup != null) {
251 log.debug("Processing NextObjective id{} in dev{} - add bucket",
252 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700253 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800254 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800255 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800256 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
257 nextObjective.id(), deviceId);
258 // by design only one pending bucket is allowed for the group
Charles Chan425854b2016-04-11 15:32:12 -0700259 groupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800260 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800261 break;
262 case REMOVE:
263 if (nextGroup == null) {
264 log.warn("Cannot remove next {} that does not exist in device {}",
265 nextObjective.id(), deviceId);
266 return;
267 }
268 log.debug("Processing NextObjective id{} in dev{} - remove group",
269 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700270 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800271 break;
272 case REMOVE_FROM_EXISTING:
273 if (nextGroup == null) {
274 log.warn("Cannot remove from next {} that does not exist in device {}",
275 nextObjective.id(), deviceId);
276 return;
277 }
278 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
279 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700280 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800281 break;
282 default:
Saurav Das4f980082015-11-05 13:39:15 -0800283 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700284 }
285 }
286
287 //////////////////////////////////////
288 // Flow handling
289 //////////////////////////////////////
290
291 /**
292 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
293 * and IP addresses configured on switch ports happen in different tables.
294 * Note that IP filtering rules need to be added to the ACL table, as there
295 * is no mechanism to send to controller via IP table.
296 *
297 * @param filt the filtering objective
298 * @param install indicates whether to add or remove the objective
299 * @param applicationId the application that sent this objective
300 */
Saurav Das52025962016-01-28 22:30:01 -0800301 protected void processFilter(FilteringObjective filt,
302 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700303 // This driver only processes filtering criteria defined with switch
304 // ports as the key
305 PortCriterion portCriterion = null;
306 EthCriterion ethCriterion = null;
307 VlanIdCriterion vidCriterion = null;
308 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
309 if (!filt.key().equals(Criteria.dummy()) &&
310 filt.key().type() == Criterion.Type.IN_PORT) {
311 portCriterion = (PortCriterion) filt.key();
312 } else {
313 log.warn("No key defined in filtering objective from app: {}. Not"
314 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700315 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700316 return;
317 }
Saurav Das59232cf2016-04-27 18:35:50 -0700318 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
319 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700320 // convert filtering conditions for switch-intfs into flowrules
321 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
322 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700323 if (criterion.type() == Criterion.Type.ETH_DST ||
324 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das822c4e22015-10-23 10:51:11 -0700325 ethCriterion = (EthCriterion) criterion;
326 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
327 vidCriterion = (VlanIdCriterion) criterion;
328 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
329 ips.add((IPCriterion) criterion);
330 } else {
331 log.error("Unsupported filter {}", criterion);
332 fail(filt, ObjectiveError.UNSUPPORTED);
333 return;
334 }
335 }
336
Saurav Das0e99e2b2015-10-28 12:39:42 -0700337 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800338 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700339 // Use the VLAN in metadata whenever a metadata is provided
340 if (filt.meta() != null) {
341 assignedVlan = readVlanFromTreatment(filt.meta());
342 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
343 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800344 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700345 }
Charles Chane849c192016-01-11 18:28:54 -0800346
Charles Chand55e84d2016-03-30 17:54:24 -0700347 if (assignedVlan == null) {
348 log.error("Driver fails to extract VLAN information. "
349 + "Not proccessing VLAN filters on device {}.", deviceId);
350 log.debug("VLAN ID in criterion={}, metadata={}",
351 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
352 fail(filt, ObjectiveError.BADPARAMS);
353 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700354 }
355 }
356
Charles Chane849c192016-01-11 18:28:54 -0800357 if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700358 // NOTE: it is possible that a filtering objective only has vidCriterion
359 log.debug("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700360 } else {
361 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700362 vidCriterion, assignedVlan,
363 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700364 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
365 tmacRule, deviceId);
366 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
367 }
368 }
369
Charles Chan985b12e2016-05-11 19:47:22 -0700370 if (vidCriterion == null) {
371 // NOTE: it is possible that a filtering objective only has ethCriterion
372 log.debug("filtering objective missing dstMac or VLAN, "
Charles Chane849c192016-01-11 18:28:54 -0800373 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700374 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800375 /*
376 * NOTE: Separate vlan filtering rules and assignment rules
377 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800378 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800379 */
380 List<FlowRule> allRules = processVlanIdFilter(
381 portCriterion, vidCriterion, assignedVlan, applicationId);
382 List<FlowRule> filteringRules = new ArrayList<>();
383 List<FlowRule> assignmentRules = new ArrayList<>();
384
385 allRules.forEach(flowRule -> {
386 ExtensionCriterion extCriterion =
387 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
388 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
Charles Chanbe8aea42016-02-24 12:04:47 -0800389 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800390 filteringRules.add(flowRule);
391 } else {
392 assignmentRules.add(flowRule);
393 }
394 });
395
396 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700397 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800398 filteringRule, deviceId);
399 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
400 }
401
402 ops.newStage();
403
404 for (FlowRule assignmentRule : assignmentRules) {
405 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
406 assignmentRule, deviceId);
407 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700408 }
409 }
410
411 for (IPCriterion ipaddr : ips) {
412 // since we ignore port information for IP rules, and the same (gateway) IP
413 // can be configured on multiple ports, we make sure that we send
414 // only a single rule to the switch.
415 if (!sentIpFilters.contains(ipaddr)) {
416 sentIpFilters.add(ipaddr);
417 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
418 ipaddr, deviceId);
419 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
420 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
421 selector.matchEthType(Ethernet.TYPE_IPV4);
422 selector.matchIPDst(ipaddr.ip());
423 treatment.setOutput(PortNumber.CONTROLLER);
424 FlowRule rule = DefaultFlowRule.builder()
425 .forDevice(deviceId)
426 .withSelector(selector.build())
427 .withTreatment(treatment.build())
428 .withPriority(HIGHEST_PRIORITY)
429 .fromApp(applicationId)
430 .makePermanent()
431 .forTable(ACL_TABLE).build();
432 ops = install ? ops.add(rule) : ops.remove(rule);
433 }
434 }
435
436 // apply filtering flow rules
437 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
438 @Override
439 public void onSuccess(FlowRuleOperations ops) {
440 log.info("Applied {} filtering rules in device {}",
441 ops.stages().get(0).size(), deviceId);
442 pass(filt);
443 }
444
445 @Override
446 public void onError(FlowRuleOperations ops) {
447 log.info("Failed to apply all filtering rules in dev {}", deviceId);
448 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
449 }
450 }));
451
452 }
453
454 /**
455 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700456 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700457 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700458 *
Charles Chanf9e98652016-09-07 16:54:23 -0700459 * @param portCriterion port on device for which this filter is programmed
460 * @param vidCriterion vlan assigned to port, or NONE for untagged
461 * @param assignedVlan assigned vlan-id for untagged packets
462 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700463 * @return list of FlowRule for port-vlan filters
464 */
465 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
466 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700467 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700468 ApplicationId applicationId) {
Charles Chanf9e98652016-09-07 16:54:23 -0700469 return processVlanIdFilterInternal(portCriterion, vidCriterion, assignedVlan,
470 applicationId, true);
471 }
472
473 /**
474 * Internal implementation of processVlanIdFilter.
475 * <p>
476 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
477 * Since it is non-OF spec, we need an extension treatment for that.
478 * The useSetVlanExtension must be set to false for OFDPA i12.
479 * </p>
480 *
481 * @param portCriterion port on device for which this filter is programmed
482 * @param vidCriterion vlan assigned to port, or NONE for untagged
483 * @param assignedVlan assigned vlan-id for untagged packets
484 * @param applicationId for application programming this filter
485 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
486 * @return list of FlowRule for port-vlan filters
487 */
488 protected List<FlowRule> processVlanIdFilterInternal(PortCriterion portCriterion,
489 VlanIdCriterion vidCriterion,
490 VlanId assignedVlan,
491 ApplicationId applicationId,
492 boolean useSetVlanExtension) {
Charles Chan14967c22015-12-07 11:11:50 -0800493 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700494 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
495 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800496 TrafficSelector.Builder preSelector = null;
497 TrafficTreatment.Builder preTreatment = null;
498
Saurav Das4f980082015-11-05 13:39:15 -0800499 treatment.transition(TMAC_TABLE);
500
Saurav Das822c4e22015-10-23 10:51:11 -0700501 if (vidCriterion.vlanId() == VlanId.NONE) {
502 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800503 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800504 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700505 if (useSetVlanExtension) {
506 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
507 treatment.extension(ofdpaSetVlanVid, deviceId);
508 } else {
509 treatment.setVlanId(assignedVlan);
510 }
Charles Chan14967c22015-12-07 11:11:50 -0800511
512 preSelector = DefaultTrafficSelector.builder();
513 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
514 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
515 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
516
Saurav Das4f980082015-11-05 13:39:15 -0800517 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800518 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
519 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700520
521 if (!assignedVlan.equals(vidCriterion.vlanId())) {
522 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
523 treatment.extension(ofdpaSetVlanVid, deviceId);
524 }
Saurav Das822c4e22015-10-23 10:51:11 -0700525 }
Saurav Das822c4e22015-10-23 10:51:11 -0700526
527 // ofdpa cannot match on ALL portnumber, so we need to use separate
528 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800529 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700530 if (portCriterion.port() == PortNumber.ALL) {
531 for (Port port : deviceService.getPorts(deviceId)) {
532 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
533 portnums.add(port.number());
534 }
535 }
536 } else {
537 portnums.add(portCriterion.port());
538 }
Saurav Das4f980082015-11-05 13:39:15 -0800539
Saurav Das822c4e22015-10-23 10:51:11 -0700540 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800541 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700542 selector.matchInPort(pnum);
543 FlowRule rule = DefaultFlowRule.builder()
544 .forDevice(deviceId)
545 .withSelector(selector.build())
546 .withTreatment(treatment.build())
547 .withPriority(DEFAULT_PRIORITY)
548 .fromApp(applicationId)
549 .makePermanent()
550 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800551
552 if (preSelector != null) {
553 preSelector.matchInPort(pnum);
554 FlowRule preRule = DefaultFlowRule.builder()
555 .forDevice(deviceId)
556 .withSelector(preSelector.build())
557 .withTreatment(preTreatment.build())
558 .withPriority(DEFAULT_PRIORITY)
559 .fromApp(applicationId)
560 .makePermanent()
561 .forTable(VLAN_TABLE).build();
562 rules.add(preRule);
563 }
564
Saurav Das822c4e22015-10-23 10:51:11 -0700565 rules.add(rule);
566 }
567 return rules;
568 }
569
570 /**
571 * Allows routed packets with correct destination MAC to be directed
572 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700573 *
574 * @param portCriterion port on device for which this filter is programmed
575 * @param ethCriterion dstMac of device for which is filter is programmed
576 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700577 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700578 * @param applicationId for application programming this filter
579 * @return list of FlowRule for port-vlan filters
580
581 */
582 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
583 EthCriterion ethCriterion,
584 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700585 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700586 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800587 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
588 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
589 return processEthDstOnlyFilter(ethCriterion, applicationId);
590 }
591
Charles Chan5b9df8d2016-03-28 22:21:40 -0700592 // Multicast MAC
593 if (ethCriterion.mask() != null) {
594 return processMcastEthDstFilter(ethCriterion, applicationId);
595 }
596
Saurav Das822c4e22015-10-23 10:51:11 -0700597 //handling untagged packets via assigned VLAN
598 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700599 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700600 }
601 // ofdpa cannot match on ALL portnumber, so we need to use separate
602 // rules for each port.
603 List<PortNumber> portnums = new ArrayList<PortNumber>();
604 if (portCriterion.port() == PortNumber.ALL) {
605 for (Port port : deviceService.getPorts(deviceId)) {
606 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
607 portnums.add(port.number());
608 }
609 }
610 } else {
611 portnums.add(portCriterion.port());
612 }
613
614 List<FlowRule> rules = new ArrayList<FlowRule>();
615 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800616 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700617 // for unicast IP packets
618 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
619 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
620 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800621 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700622 selector.matchEthType(Ethernet.TYPE_IPV4);
623 selector.matchEthDst(ethCriterion.mac());
624 treatment.transition(UNICAST_ROUTING_TABLE);
625 FlowRule rule = DefaultFlowRule.builder()
626 .forDevice(deviceId)
627 .withSelector(selector.build())
628 .withTreatment(treatment.build())
629 .withPriority(DEFAULT_PRIORITY)
630 .fromApp(applicationId)
631 .makePermanent()
632 .forTable(TMAC_TABLE).build();
633 rules.add(rule);
634 //for MPLS packets
635 selector = DefaultTrafficSelector.builder();
636 treatment = DefaultTrafficTreatment.builder();
637 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800638 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700639 selector.matchEthType(Ethernet.MPLS_UNICAST);
640 selector.matchEthDst(ethCriterion.mac());
641 treatment.transition(MPLS_TABLE_0);
642 rule = DefaultFlowRule.builder()
643 .forDevice(deviceId)
644 .withSelector(selector.build())
645 .withTreatment(treatment.build())
646 .withPriority(DEFAULT_PRIORITY)
647 .fromApp(applicationId)
648 .makePermanent()
649 .forTable(TMAC_TABLE).build();
650 rules.add(rule);
651 }
652 return rules;
653 }
654
Charles Chan5270ed02016-01-30 23:22:37 -0800655 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
656 ApplicationId applicationId) {
657 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
658 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
659 selector.matchEthType(Ethernet.TYPE_IPV4);
660 selector.matchEthDst(ethCriterion.mac());
661 treatment.transition(UNICAST_ROUTING_TABLE);
662 FlowRule rule = DefaultFlowRule.builder()
663 .forDevice(deviceId)
664 .withSelector(selector.build())
665 .withTreatment(treatment.build())
666 .withPriority(DEFAULT_PRIORITY)
667 .fromApp(applicationId)
668 .makePermanent()
669 .forTable(TMAC_TABLE).build();
670 return ImmutableList.<FlowRule>builder().add(rule).build();
671 }
672
Charles Chan5b9df8d2016-03-28 22:21:40 -0700673 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
674 ApplicationId applicationId) {
675 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
676 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
677 selector.matchEthType(Ethernet.TYPE_IPV4);
678 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
679 treatment.transition(MULTICAST_ROUTING_TABLE);
680 FlowRule rule = DefaultFlowRule.builder()
681 .forDevice(deviceId)
682 .withSelector(selector.build())
683 .withTreatment(treatment.build())
684 .withPriority(DEFAULT_PRIORITY)
685 .fromApp(applicationId)
686 .makePermanent()
687 .forTable(TMAC_TABLE).build();
688 return ImmutableList.<FlowRule>builder().add(rule).build();
689 }
690
Saurav Das822c4e22015-10-23 10:51:11 -0700691 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
692 switch (fwd.flag()) {
693 case SPECIFIC:
694 return processSpecific(fwd);
695 case VERSATILE:
696 return processVersatile(fwd);
697 default:
698 fail(fwd, ObjectiveError.UNKNOWN);
699 log.warn("Unknown forwarding flag {}", fwd.flag());
700 }
701 return Collections.emptySet();
702 }
703
704 /**
705 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
706 * ACL table.
707 * @param fwd the forwarding objective of type 'versatile'
708 * @return a collection of flow rules to be sent to the switch. An empty
709 * collection may be returned if there is a problem in processing
710 * the flow rule
711 */
Saurav Das52025962016-01-28 22:30:01 -0800712 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -0700713 log.info("Processing versatile forwarding objective");
Saurav Das822c4e22015-10-23 10:51:11 -0700714
715 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800716 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700717 if (ethType == null) {
718 log.error("Versatile forwarding objective must include ethType");
719 fail(fwd, ObjectiveError.BADPARAMS);
720 return Collections.emptySet();
721 }
722 if (fwd.nextId() == null && fwd.treatment() == null) {
723 log.error("Forwarding objective {} from {} must contain "
724 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800725 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700726 return Collections.emptySet();
727 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800728
Saurav Das77b5e902016-01-27 17:01:59 -0800729 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
730 fwd.selector().criteria().forEach(criterion -> {
731 if (criterion instanceof VlanIdCriterion) {
732 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
733 // ensure that match does not include vlan = NONE as OF-DPA does not
734 // match untagged packets this way in the ACL table.
735 if (vlanId.equals(VlanId.NONE)) {
736 return;
737 }
738 OfdpaMatchVlanVid ofdpaMatchVlanVid =
739 new OfdpaMatchVlanVid(vlanId);
740 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
741 } else {
742 sbuilder.add(criterion);
743 }
744 });
745
Saurav Das822c4e22015-10-23 10:51:11 -0700746 // XXX driver does not currently do type checking as per Tables 65-67 in
747 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800748 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
749 if (fwd.treatment() != null) {
750 for (Instruction ins : fwd.treatment().allInstructions()) {
751 if (ins instanceof OutputInstruction) {
752 OutputInstruction o = (OutputInstruction) ins;
753 if (o.port() == PortNumber.CONTROLLER) {
754 ttBuilder.add(o);
755 } else {
756 log.warn("Only allowed treatments in versatile forwarding "
757 + "objectives are punts to the controller");
758 }
759 } else {
760 log.warn("Cannot process instruction in versatile fwd {}", ins);
761 }
Saurav Das822c4e22015-10-23 10:51:11 -0700762 }
763 }
Saurav Das822c4e22015-10-23 10:51:11 -0700764 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800765 // overide case
766 NextGroup next = getGroupForNextObjective(fwd.nextId());
767 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
768 // we only need the top level group's key to point the flow to it
769 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
770 if (group == null) {
771 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
772 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
773 fail(fwd, ObjectiveError.GROUPMISSING);
774 return Collections.emptySet();
775 }
776 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700777 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800778
779 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
780 .fromApp(fwd.appId())
781 .withPriority(fwd.priority())
782 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800783 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800784 .withTreatment(ttBuilder.build())
785 .makePermanent()
786 .forTable(ACL_TABLE);
787 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700788 }
789
790 /**
791 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800792 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700793 *
794 * @param fwd the forwarding objective of type 'specific'
795 * @return a collection of flow rules. Typically there will be only one
796 * for this type of forwarding objective. An empty set may be
797 * returned if there is an issue in processing the objective.
798 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800799 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700800 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800801 fwd.id(), deviceId, fwd.nextId());
802 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
803 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
804
805 if (isEthTypeObj) {
806 return processEthTypeSpecific(fwd);
807 } else if (isEthDstObj) {
808 return processEthDstSpecific(fwd);
809 } else {
810 log.warn("processSpecific: Unsupported forwarding objective "
811 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700812 fail(fwd, ObjectiveError.UNSUPPORTED);
813 return Collections.emptySet();
814 }
Saurav Das4ce45962015-11-24 23:21:05 -0800815 }
816
Saurav Das4ce45962015-11-24 23:21:05 -0800817 /**
818 * Handles forwarding rules to the IP and MPLS tables.
819 *
820 * @param fwd the forwarding objective
821 * @return A collection of flow rules, or an empty set
822 */
823 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700824 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700825 }
826
827 /**
828 * Internal implementation of processEthTypeSpecific.
829 * <p>
830 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
831 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
832 * The allowDefaultRoute must be set to false for OFDPA i12.
833 * </p>
834 *
835 * @param fwd the forwarding objective
836 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
837 * @return A collection of flow rules, or an empty set
838 */
839 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -0700840 boolean allowDefaultRoute,
841 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -0800842 TrafficSelector selector = fwd.selector();
843 EthTypeCriterion ethType =
844 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800845 boolean defaultRule = false;
846 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700847 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800848 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800849 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800850 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800851 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800852
Saurav Das8a0732e2015-11-20 15:27:53 -0800853 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800854 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700855 if (ipv4Dst.isMulticast()) {
856 if (ipv4Dst.prefixLength() != 32) {
857 log.warn("Multicast specific forwarding objective can only be /32");
858 fail(fwd, ObjectiveError.BADPARAMS);
859 return ImmutableSet.of();
860 }
861 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
862 if (assignedVlan == null) {
863 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
864 fail(fwd, ObjectiveError.BADPARAMS);
865 return ImmutableSet.of();
866 }
867 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
868 filteredSelector.extension(ofdpaMatchVlanVid, deviceId);
869 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
870 forTableId = MULTICAST_ROUTING_TABLE;
871 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
872 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800873 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700874 if (ipv4Dst.prefixLength() == 0) {
875 if (allowDefaultRoute) {
876 // The entire IPV4_DST field is wildcarded intentionally
877 filteredSelector.matchEthType(Ethernet.TYPE_IPV4);
878 } else {
879 /*
880 * NOTE: The switch does not support matching 0.0.0.0/0
881 * Split it into 0.0.0.0/1 and 128.0.0.0/1
882 */
883 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
884 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
885 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
886 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
887 defaultRule = true;
888 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700889 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700890 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700891 }
892 forTableId = UNICAST_ROUTING_TABLE;
893 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
894 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800895 }
Charles Chan14967c22015-12-07 11:11:50 -0800896
897 if (fwd.treatment() != null) {
898 for (Instruction instr : fwd.treatment().allInstructions()) {
899 if (instr instanceof L3ModificationInstruction &&
900 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
901 tb.deferred().add(instr);
902 }
903 }
904 }
905
Saurav Das8a0732e2015-11-20 15:27:53 -0800906 } else {
907 filteredSelector
908 .matchEthType(Ethernet.MPLS_UNICAST)
909 .matchMplsLabel(((MplsCriterion)
910 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
911 MplsBosCriterion bos = (MplsBosCriterion) selector
912 .getCriterion(Criterion.Type.MPLS_BOS);
913 if (bos != null) {
914 filteredSelector.matchMplsBos(bos.mplsBos());
915 }
916 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800917 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
918 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700919
Charles Chan14967c22015-12-07 11:11:50 -0800920 if (fwd.treatment() != null) {
921 for (Instruction instr : fwd.treatment().allInstructions()) {
922 if (instr instanceof L2ModificationInstruction &&
923 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800924 popMpls = true;
Charles Chan14967c22015-12-07 11:11:50 -0800925 tb.immediate().add(instr);
926 }
927 if (instr instanceof L3ModificationInstruction &&
928 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
929 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
930 tb.immediate().decMplsTtl();
931 }
932 if (instr instanceof L3ModificationInstruction &&
933 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
934 tb.immediate().add(instr);
935 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800936 }
937 }
938 }
Saurav Das822c4e22015-10-23 10:51:11 -0700939
940 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800941 if (forTableId == MPLS_TABLE_1 && !popMpls) {
942 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -0700943 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
944 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800945 // XXX We could convert to forwarding to a single-port, via a
946 // MPLS interface, or a MPLS SWAP (with-same) but that would
947 // have to be handled in the next-objective. Also the pop-mpls
948 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800949 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800950 return Collections.emptySet();
951 }
952
Saurav Das423fe2b2015-12-04 10:52:59 -0800953 NextGroup next = getGroupForNextObjective(fwd.nextId());
954 if (next != null) {
955 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
956 // we only need the top level group's key to point the flow to it
957 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
958 if (group == null) {
959 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
960 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
961 fail(fwd, ObjectiveError.GROUPMISSING);
962 return Collections.emptySet();
963 }
964 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700965 // check if group is empty
966 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
967 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
968 Integer.toHexString(group.id().id()), deviceId, fwd.id());
969 emptyGroup = true;
970 }
Saurav Das25190812016-05-27 13:54:07 -0700971 } else {
972 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
973 fwd.nextId(), deviceId, fwd.id());
974 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
975 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -0700976 }
Saurav Das822c4e22015-10-23 10:51:11 -0700977 }
Charles Chancad338a2016-09-16 18:03:11 -0700978
979 if (forTableId == MPLS_TABLE_1) {
980 if (mplsNextTable == MPLS_L3_TYPE) {
981 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
982 tb.extension(setMplsType, deviceId);
983 }
984 tb.transition(mplsNextTable);
985 } else {
986 tb.transition(ACL_TABLE);
987 }
988
Saurav Das822c4e22015-10-23 10:51:11 -0700989 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
990 .fromApp(fwd.appId())
991 .withPriority(fwd.priority())
992 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800993 .withSelector(filteredSelector.build())
994 .withTreatment(tb.build())
995 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700996
997 if (fwd.permanent()) {
998 ruleBuilder.makePermanent();
999 } else {
1000 ruleBuilder.makeTemporary(fwd.timeout());
1001 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001002 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1003 flowRuleCollection.add(ruleBuilder.build());
1004 if (defaultRule) {
1005 FlowRule.Builder rule = DefaultFlowRule.builder()
1006 .fromApp(fwd.appId())
1007 .withPriority(fwd.priority())
1008 .forDevice(deviceId)
1009 .withSelector(complementarySelector.build())
1010 .withTreatment(tb.build())
1011 .forTable(forTableId);
1012 if (fwd.permanent()) {
1013 rule.makePermanent();
1014 } else {
1015 rule.makeTemporary(fwd.timeout());
1016 }
1017 flowRuleCollection.add(rule.build());
1018 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1019 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001020 // XXX retrying flows may be necessary due to bug CORD-554
1021 if (emptyGroup) {
1022 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1023 RETRY_MS, TimeUnit.MILLISECONDS);
1024 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001025 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001026 }
1027
Saurav Das4ce45962015-11-24 23:21:05 -08001028 /**
1029 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1030 * allowed in the bridging table - instead we use L2 Interface group or
1031 * L2 flood group
1032 *
1033 * @param fwd the forwarding objective
1034 * @return A collection of flow rules, or an empty set
1035 */
1036 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1037 List<FlowRule> rules = new ArrayList<>();
1038
1039 // Build filtered selector
1040 TrafficSelector selector = fwd.selector();
1041 EthCriterion ethCriterion = (EthCriterion) selector
1042 .getCriterion(Criterion.Type.ETH_DST);
1043 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1044 .getCriterion(Criterion.Type.VLAN_VID);
1045
1046 if (vlanIdCriterion == null) {
1047 log.warn("Forwarding objective for bridging requires vlan. Not "
1048 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1049 fail(fwd, ObjectiveError.BADPARAMS);
1050 return Collections.emptySet();
1051 }
1052
1053 TrafficSelector.Builder filteredSelectorBuilder =
1054 DefaultTrafficSelector.builder();
1055 // Do not match MacAddress for subnet broadcast entry
1056 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
1057 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1058 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1059 fwd.id(), fwd.nextId(), deviceId);
1060 } else {
1061 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1062 + "in dev:{} for vlan:{}",
1063 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1064 }
Charles Chan14967c22015-12-07 11:11:50 -08001065 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1066 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -08001067 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1068
1069 if (fwd.treatment() != null) {
1070 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1071 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1072 }
1073
1074 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1075 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001076 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001077 if (next != null) {
1078 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1079 // we only need the top level group's key to point the flow to it
1080 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1081 if (group != null) {
1082 treatmentBuilder.deferred().group(group.id());
1083 } else {
1084 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1085 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1086 fail(fwd, ObjectiveError.GROUPMISSING);
1087 return Collections.emptySet();
1088 }
1089 }
1090 }
1091 treatmentBuilder.immediate().transition(ACL_TABLE);
1092 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1093
1094 // Build bridging table entries
1095 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1096 flowRuleBuilder.fromApp(fwd.appId())
1097 .withPriority(fwd.priority())
1098 .forDevice(deviceId)
1099 .withSelector(filteredSelector)
1100 .withTreatment(filteredTreatment)
1101 .forTable(BRIDGING_TABLE);
1102 if (fwd.permanent()) {
1103 flowRuleBuilder.makePermanent();
1104 } else {
1105 flowRuleBuilder.makeTemporary(fwd.timeout());
1106 }
1107 rules.add(flowRuleBuilder.build());
1108 return rules;
1109 }
1110
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001111 //////////////////////////////////////
1112 // Helper Methods and Classes
1113 //////////////////////////////////////
1114
1115 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1116 TrafficSelector selector = fwd.selector();
1117 EthTypeCriterion ethType = (EthTypeCriterion) selector
1118 .getCriterion(Criterion.Type.ETH_TYPE);
1119 return !((ethType == null) ||
1120 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
1121 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
1122 }
1123
1124 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1125 TrafficSelector selector = fwd.selector();
1126 EthCriterion ethDst = (EthCriterion) selector
1127 .getCriterion(Criterion.Type.ETH_DST);
1128 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1129 .getCriterion(Criterion.Type.VLAN_VID);
1130 return !(ethDst == null && vlanId == null);
1131 }
1132
Saurav Das423fe2b2015-12-04 10:52:59 -08001133 protected NextGroup getGroupForNextObjective(Integer nextId) {
1134 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1135 if (next != null) {
1136 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1137 if (gkeys != null && !gkeys.isEmpty()) {
1138 return next;
1139 } else {
1140 log.warn("Empty next group found in FlowObjective store for "
1141 + "next-id:{} in dev:{}", nextId, deviceId);
1142 }
1143 } else {
1144 log.warn("next-id {} not found in Flow objective store for dev:{}",
1145 nextId, deviceId);
1146 }
1147 return null;
1148 }
1149
Charles Chan188ebf52015-12-23 00:15:11 -08001150 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001151 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001152 }
1153
Charles Chan188ebf52015-12-23 00:15:11 -08001154 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001155 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001156 }
Saurav Das24431192016-03-07 19:13:00 -08001157
Saurav Das24431192016-03-07 19:13:00 -08001158 @Override
1159 public List<String> getNextMappings(NextGroup nextGroup) {
1160 List<String> mappings = new ArrayList<>();
1161 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1162 for (Deque<GroupKey> gkd : gkeys) {
1163 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001164 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001165 for (GroupKey gk : gkd) {
1166 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001167 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001168 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001169 continue;
1170 }
1171 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1172 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001173 lastGroup = g;
1174 }
1175 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001176 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001177 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001178 lastGroupIns = lastGroup.buckets().buckets().get(0)
1179 .treatment().allInstructions();
1180 }
1181 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001182 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001183 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001184 }
1185 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001186 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001187 }
1188 return mappings;
1189 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001190
1191 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001192 if (selector == null) {
1193 return null;
1194 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001195 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1196 return (criterion == null)
1197 ? null : ((VlanIdCriterion) criterion).vlanId();
1198 }
1199
1200 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001201 if (selector == null) {
1202 return null;
1203 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001204 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1205 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1206 }
Charles Chand55e84d2016-03-30 17:54:24 -07001207
1208 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001209 if (treatment == null) {
1210 return null;
1211 }
Charles Chand55e84d2016-03-30 17:54:24 -07001212 for (Instruction i : treatment.allInstructions()) {
1213 if (i instanceof ModVlanIdInstruction) {
1214 return ((ModVlanIdInstruction) i).vlanId();
1215 }
1216 }
1217 return null;
1218 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001219
1220 /**
1221 * Utility class that retries sending flows a fixed number of times, even if
1222 * some of the attempts are successful. Used only for forwarding objectives.
1223 */
1224 protected final class RetryFlows implements Runnable {
1225 int attempts = MAX_RETRY_ATTEMPTS;
1226 private Collection<FlowRule> retryFlows;
1227 private ForwardingObjective fwd;
1228
1229 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1230 this.fwd = fwd;
1231 this.retryFlows = retryFlows;
1232 }
1233
1234 @Override
1235 public void run() {
1236 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1237 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1238 sendForward(fwd, retryFlows);
1239 if (--attempts > 0) {
1240 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1241 }
1242 }
1243 }
1244
Saurav Das822c4e22015-10-23 10:51:11 -07001245}