blob: c507e9c1f8292aa26cb5b39d8d49aedcde6c2ffb [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 Das822c4e22015-10-23 10:51:11 -070018import static org.slf4j.LoggerFactory.getLogger;
19
Saurav Das8a0732e2015-11-20 15:27:53 -080020import java.util.ArrayDeque;
Saurav Das822c4e22015-10-23 10:51:11 -070021import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
Saurav Das8a0732e2015-11-20 15:27:53 -080024import java.util.Deque;
Saurav Das822c4e22015-10-23 10:51:11 -070025import java.util.List;
26import java.util.Set;
27import java.util.concurrent.ConcurrentHashMap;
Saurav Das822c4e22015-10-23 10:51:11 -070028
29import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070030import org.onlab.packet.Ethernet;
Saurav Das822c4e22015-10-23 10:51:11 -070031import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onlab.packet.VlanId;
33import org.onlab.util.KryoNamespace;
34import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
Charles Chan188ebf52015-12-23 00:15:11 -080036import org.onosproject.driver.pipeline.OFDPA2GroupHandler.OfdpaNextGroup;
Saurav Das822c4e22015-10-23 10:51:11 -070037import org.onosproject.net.DeviceId;
38import org.onosproject.net.Port;
39import org.onosproject.net.PortNumber;
40import org.onosproject.net.behaviour.NextGroup;
41import org.onosproject.net.behaviour.Pipeliner;
42import org.onosproject.net.behaviour.PipelinerContext;
43import org.onosproject.net.device.DeviceService;
44import org.onosproject.net.driver.AbstractHandlerBehaviour;
45import org.onosproject.net.flow.DefaultFlowRule;
46import org.onosproject.net.flow.DefaultTrafficSelector;
47import org.onosproject.net.flow.DefaultTrafficTreatment;
48import org.onosproject.net.flow.FlowRule;
49import org.onosproject.net.flow.FlowRuleOperations;
50import org.onosproject.net.flow.FlowRuleOperationsContext;
51import org.onosproject.net.flow.FlowRuleService;
52import org.onosproject.net.flow.TrafficSelector;
53import org.onosproject.net.flow.TrafficTreatment;
54import org.onosproject.net.flow.criteria.Criteria;
55import org.onosproject.net.flow.criteria.Criterion;
56import org.onosproject.net.flow.criteria.EthCriterion;
57import org.onosproject.net.flow.criteria.EthTypeCriterion;
58import org.onosproject.net.flow.criteria.IPCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080059import org.onosproject.net.flow.criteria.MplsBosCriterion;
60import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070061import org.onosproject.net.flow.criteria.PortCriterion;
62import org.onosproject.net.flow.criteria.VlanIdCriterion;
63import org.onosproject.net.flow.instructions.Instruction;
64import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
65import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080066import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
68import 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
Saurav Das822c4e22015-10-23 10:51:11 -070081/**
82 * Driver for Broadcom's OF-DPA v2.0 TTP.
83 *
84 */
85public class OFDPA2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -070086 protected static final int PORT_TABLE = 0;
87 protected static final int VLAN_TABLE = 10;
88 protected static final int TMAC_TABLE = 20;
89 protected static final int UNICAST_ROUTING_TABLE = 30;
90 protected static final int MULTICAST_ROUTING_TABLE = 40;
91 protected static final int MPLS_TABLE_0 = 23;
92 protected static final int MPLS_TABLE_1 = 24;
93 protected static final int BRIDGING_TABLE = 50;
94 protected static final int ACL_TABLE = 60;
95 protected static final int MAC_LEARNING_TABLE = 254;
96 protected static final long OFPP_MAX = 0xffffff00L;
97
98 private static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -080099 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700100 protected static final int LOWEST_PRIORITY = 0x0;
101
Saurav Das822c4e22015-10-23 10:51:11 -0700102 private final Logger log = getLogger(getClass());
103 private ServiceDirectory serviceDirectory;
104 protected FlowRuleService flowRuleService;
105 private CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800106 protected GroupService groupService;
107 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700108 protected DeviceId deviceId;
109 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700110 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800111 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700112 .register(KryoNamespaces.API)
113 .register(GroupKey.class)
114 .register(DefaultGroupKey.class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800115 .register(OfdpaNextGroup.class)
Saurav Das822c4e22015-10-23 10:51:11 -0700116 .register(byte[].class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800117 .register(ArrayDeque.class)
Saurav Das822c4e22015-10-23 10:51:11 -0700118 .build();
119
Charles Chan188ebf52015-12-23 00:15:11 -0800120 protected OFDPA2GroupHandler ofdpa2GroupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700121
Saurav Das822c4e22015-10-23 10:51:11 -0700122 private Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800123 new ConcurrentHashMap<>());
Saurav Das4f980082015-11-05 13:39:15 -0800124
Saurav Das822c4e22015-10-23 10:51:11 -0700125 @Override
126 public void init(DeviceId deviceId, PipelinerContext context) {
127 this.serviceDirectory = context.directory();
128 this.deviceId = deviceId;
129
Charles Chan188ebf52015-12-23 00:15:11 -0800130 // Initialize OFDPA group handler
131 ofdpa2GroupHandler = new OFDPA2GroupHandler();
132 ofdpa2GroupHandler.init(deviceId, context);
Saurav Das822c4e22015-10-23 10:51:11 -0700133
134 coreService = serviceDirectory.get(CoreService.class);
135 flowRuleService = serviceDirectory.get(FlowRuleService.class);
136 groupService = serviceDirectory.get(GroupService.class);
137 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700138 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700139
140 driverId = coreService.registerApplication(
141 "org.onosproject.driver.OFDPA2Pipeline");
142
Saurav Das822c4e22015-10-23 10:51:11 -0700143 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700144 }
145
146 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800147 // OF-DPA does not require initializing the pipeline as it puts default
148 // rules automatically in the hardware. However emulation of OFDPA in
149 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700150 }
151
152 //////////////////////////////////////
153 // Flow Objectives
154 //////////////////////////////////////
155
156 @Override
157 public void filter(FilteringObjective filteringObjective) {
158 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
159 processFilter(filteringObjective,
160 filteringObjective.op() == Objective.Operation.ADD,
161 filteringObjective.appId());
162 } else {
163 // Note that packets that don't match the PERMIT filter are
164 // automatically denied. The DENY filter is used to deny packets
165 // that are otherwise permitted by the PERMIT filter.
166 // Use ACL table flow rules here for DENY filtering objectives
167 log.debug("filter objective other than PERMIT currently not supported");
168 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
169 }
170 }
171
172 @Override
173 public void forward(ForwardingObjective fwd) {
174 Collection<FlowRule> rules;
175 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
176
177 rules = processForward(fwd);
178 switch (fwd.op()) {
179 case ADD:
180 rules.stream()
181 .filter(rule -> rule != null)
182 .forEach(flowOpsBuilder::add);
183 break;
184 case REMOVE:
185 rules.stream()
186 .filter(rule -> rule != null)
187 .forEach(flowOpsBuilder::remove);
188 break;
189 default:
190 fail(fwd, ObjectiveError.UNKNOWN);
191 log.warn("Unknown forwarding type {}", fwd.op());
192 }
193
Saurav Das822c4e22015-10-23 10:51:11 -0700194 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
195 @Override
196 public void onSuccess(FlowRuleOperations ops) {
197 pass(fwd);
198 }
199
200 @Override
201 public void onError(FlowRuleOperations ops) {
202 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
203 }
204 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700205 }
206
207 @Override
208 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800209 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
210 switch (nextObjective.op()) {
211 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800212 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800213 log.warn("Cannot add next {} that already exists in device {}",
214 nextObjective.id(), deviceId);
215 return;
216 }
217 log.debug("Processing NextObjective id{} in dev{} - add group",
218 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800219 ofdpa2GroupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800220 break;
221 case ADD_TO_EXISTING:
222 if (nextGroup != null) {
223 log.debug("Processing NextObjective id{} in dev{} - add bucket",
224 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800225 ofdpa2GroupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800226 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800227 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800228 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
229 nextObjective.id(), deviceId);
230 // by design only one pending bucket is allowed for the group
Charles Chan188ebf52015-12-23 00:15:11 -0800231 ofdpa2GroupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800232 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800233 break;
234 case REMOVE:
235 if (nextGroup == null) {
236 log.warn("Cannot remove next {} that does not exist in device {}",
237 nextObjective.id(), deviceId);
238 return;
239 }
240 log.debug("Processing NextObjective id{} in dev{} - remove group",
241 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800242 ofdpa2GroupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800243 break;
244 case REMOVE_FROM_EXISTING:
245 if (nextGroup == null) {
246 log.warn("Cannot remove from next {} that does not exist in device {}",
247 nextObjective.id(), deviceId);
248 return;
249 }
250 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
251 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800252 ofdpa2GroupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800253 break;
254 default:
Saurav Das4f980082015-11-05 13:39:15 -0800255 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700256 }
257 }
258
259 //////////////////////////////////////
260 // Flow handling
261 //////////////////////////////////////
262
263 /**
264 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
265 * and IP addresses configured on switch ports happen in different tables.
266 * Note that IP filtering rules need to be added to the ACL table, as there
267 * is no mechanism to send to controller via IP table.
268 *
269 * @param filt the filtering objective
270 * @param install indicates whether to add or remove the objective
271 * @param applicationId the application that sent this objective
272 */
273 private void processFilter(FilteringObjective filt,
274 boolean install, ApplicationId applicationId) {
275 // This driver only processes filtering criteria defined with switch
276 // ports as the key
277 PortCriterion portCriterion = null;
278 EthCriterion ethCriterion = null;
279 VlanIdCriterion vidCriterion = null;
280 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
281 if (!filt.key().equals(Criteria.dummy()) &&
282 filt.key().type() == Criterion.Type.IN_PORT) {
283 portCriterion = (PortCriterion) filt.key();
284 } else {
285 log.warn("No key defined in filtering objective from app: {}. Not"
286 + "processing filtering objective", applicationId);
287 fail(filt, ObjectiveError.UNKNOWN);
288 return;
289 }
290 // convert filtering conditions for switch-intfs into flowrules
291 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
292 for (Criterion criterion : filt.conditions()) {
293 if (criterion.type() == Criterion.Type.ETH_DST) {
294 ethCriterion = (EthCriterion) criterion;
295 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
296 vidCriterion = (VlanIdCriterion) criterion;
297 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
298 ips.add((IPCriterion) criterion);
299 } else {
300 log.error("Unsupported filter {}", criterion);
301 fail(filt, ObjectiveError.UNSUPPORTED);
302 return;
303 }
304 }
305
Saurav Das0e99e2b2015-10-28 12:39:42 -0700306 VlanId assignedVlan = null;
307 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
308 // untagged packets are assigned vlans in OF-DPA
309 if (filt.meta() == null) {
310 log.error("Missing metadata in filtering objective required "
311 + "for vlan assignment in dev {}", deviceId);
312 fail(filt, ObjectiveError.BADPARAMS);
313 return;
314 }
315 for (Instruction i : filt.meta().allInstructions()) {
316 if (i instanceof ModVlanIdInstruction) {
317 assignedVlan = ((ModVlanIdInstruction) i).vlanId();
318 }
319 }
320 if (assignedVlan == null) {
321 log.error("Driver requires an assigned vlan-id to tag incoming "
322 + "untagged packets. Not processing vlan filters on "
323 + "device {}", deviceId);
324 fail(filt, ObjectiveError.BADPARAMS);
325 return;
326 }
327 }
328
Saurav Das822c4e22015-10-23 10:51:11 -0700329 if (ethCriterion == null) {
330 log.debug("filtering objective missing dstMac, cannot program TMAC table");
331 } else {
332 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700333 vidCriterion, assignedVlan,
334 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700335 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
336 tmacRule, deviceId);
337 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
338 }
339 }
340
341 if (ethCriterion == null || vidCriterion == null) {
342 log.debug("filtering objective missing dstMac or vlan, cannot program"
343 + "Vlan Table");
344 } else {
345 for (FlowRule vlanRule : processVlanIdFilter(portCriterion, vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700346 assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700347 applicationId)) {
348 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
349 vlanRule, deviceId);
350 ops = install ? ops.add(vlanRule) : ops.remove(vlanRule);
351 }
352 }
353
354 for (IPCriterion ipaddr : ips) {
355 // since we ignore port information for IP rules, and the same (gateway) IP
356 // can be configured on multiple ports, we make sure that we send
357 // only a single rule to the switch.
358 if (!sentIpFilters.contains(ipaddr)) {
359 sentIpFilters.add(ipaddr);
360 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
361 ipaddr, deviceId);
362 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
363 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
364 selector.matchEthType(Ethernet.TYPE_IPV4);
365 selector.matchIPDst(ipaddr.ip());
366 treatment.setOutput(PortNumber.CONTROLLER);
367 FlowRule rule = DefaultFlowRule.builder()
368 .forDevice(deviceId)
369 .withSelector(selector.build())
370 .withTreatment(treatment.build())
371 .withPriority(HIGHEST_PRIORITY)
372 .fromApp(applicationId)
373 .makePermanent()
374 .forTable(ACL_TABLE).build();
375 ops = install ? ops.add(rule) : ops.remove(rule);
376 }
377 }
378
379 // apply filtering flow rules
380 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
381 @Override
382 public void onSuccess(FlowRuleOperations ops) {
383 log.info("Applied {} filtering rules in device {}",
384 ops.stages().get(0).size(), deviceId);
385 pass(filt);
386 }
387
388 @Override
389 public void onError(FlowRuleOperations ops) {
390 log.info("Failed to apply all filtering rules in dev {}", deviceId);
391 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
392 }
393 }));
394
395 }
396
397 /**
398 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700399 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700400 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700401 *
Saurav Das822c4e22015-10-23 10:51:11 -0700402 * @param portCriterion port on device for which this filter is programmed
403 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700404 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700405 * @param applicationId for application programming this filter
406 * @return list of FlowRule for port-vlan filters
407 */
408 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
409 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700410 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700411 ApplicationId applicationId) {
412 List<FlowRule> rules = new ArrayList<FlowRule>();
413 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
414 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
415 selector.matchVlanId(vidCriterion.vlanId());
Saurav Das4f980082015-11-05 13:39:15 -0800416 treatment.transition(TMAC_TABLE);
417
418 VlanId storeVlan = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700419 if (vidCriterion.vlanId() == VlanId.NONE) {
420 // untagged packets are assigned vlans
Saurav Das0e99e2b2015-10-28 12:39:42 -0700421 treatment.pushVlan().setVlanId(assignedVlan);
Saurav Das2857f382015-11-03 14:39:27 -0800422 // XXX ofdpa will require an additional vlan match on the assigned vlan
423 // and it may not require the push. This is not in compliance with OF
424 // standard. Waiting on what the exact flows are going to look like.
Saurav Das4f980082015-11-05 13:39:15 -0800425 storeVlan = assignedVlan;
426 } else {
427 storeVlan = vidCriterion.vlanId();
Saurav Das822c4e22015-10-23 10:51:11 -0700428 }
Saurav Das822c4e22015-10-23 10:51:11 -0700429
430 // ofdpa cannot match on ALL portnumber, so we need to use separate
431 // rules for each port.
432 List<PortNumber> portnums = new ArrayList<PortNumber>();
433 if (portCriterion.port() == PortNumber.ALL) {
434 for (Port port : deviceService.getPorts(deviceId)) {
435 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
436 portnums.add(port.number());
437 }
438 }
439 } else {
440 portnums.add(portCriterion.port());
441 }
Saurav Das4f980082015-11-05 13:39:15 -0800442
Saurav Das822c4e22015-10-23 10:51:11 -0700443 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800444 // update storage
Charles Chan188ebf52015-12-23 00:15:11 -0800445 ofdpa2GroupHandler.port2Vlan.put(pnum, storeVlan);
446 Set<PortNumber> vlanPorts = ofdpa2GroupHandler.vlan2Port.get(storeVlan);
Saurav Das4f980082015-11-05 13:39:15 -0800447 if (vlanPorts == null) {
448 vlanPorts = Collections.newSetFromMap(
449 new ConcurrentHashMap<PortNumber, Boolean>());
450 vlanPorts.add(pnum);
Charles Chan188ebf52015-12-23 00:15:11 -0800451 ofdpa2GroupHandler.vlan2Port.put(storeVlan, vlanPorts);
Saurav Das4f980082015-11-05 13:39:15 -0800452 } else {
453 vlanPorts.add(pnum);
454 }
455 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700456 selector.matchInPort(pnum);
457 FlowRule rule = DefaultFlowRule.builder()
458 .forDevice(deviceId)
459 .withSelector(selector.build())
460 .withTreatment(treatment.build())
461 .withPriority(DEFAULT_PRIORITY)
462 .fromApp(applicationId)
463 .makePermanent()
464 .forTable(VLAN_TABLE).build();
465 rules.add(rule);
466 }
467 return rules;
468 }
469
470 /**
471 * Allows routed packets with correct destination MAC to be directed
472 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700473 *
474 * @param portCriterion port on device for which this filter is programmed
475 * @param ethCriterion dstMac of device for which is filter is programmed
476 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700477 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700478 * @param applicationId for application programming this filter
479 * @return list of FlowRule for port-vlan filters
480
481 */
482 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
483 EthCriterion ethCriterion,
484 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700485 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700486 ApplicationId applicationId) {
487 //handling untagged packets via assigned VLAN
488 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700489 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700490 }
491 // ofdpa cannot match on ALL portnumber, so we need to use separate
492 // rules for each port.
493 List<PortNumber> portnums = new ArrayList<PortNumber>();
494 if (portCriterion.port() == PortNumber.ALL) {
495 for (Port port : deviceService.getPorts(deviceId)) {
496 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
497 portnums.add(port.number());
498 }
499 }
500 } else {
501 portnums.add(portCriterion.port());
502 }
503
504 List<FlowRule> rules = new ArrayList<FlowRule>();
505 for (PortNumber pnum : portnums) {
506 // for unicast IP packets
507 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
508 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
509 selector.matchInPort(pnum);
510 selector.matchVlanId(vidCriterion.vlanId());
511 selector.matchEthType(Ethernet.TYPE_IPV4);
512 selector.matchEthDst(ethCriterion.mac());
513 treatment.transition(UNICAST_ROUTING_TABLE);
514 FlowRule rule = DefaultFlowRule.builder()
515 .forDevice(deviceId)
516 .withSelector(selector.build())
517 .withTreatment(treatment.build())
518 .withPriority(DEFAULT_PRIORITY)
519 .fromApp(applicationId)
520 .makePermanent()
521 .forTable(TMAC_TABLE).build();
522 rules.add(rule);
523 //for MPLS packets
524 selector = DefaultTrafficSelector.builder();
525 treatment = DefaultTrafficTreatment.builder();
526 selector.matchInPort(pnum);
527 selector.matchVlanId(vidCriterion.vlanId());
528 selector.matchEthType(Ethernet.MPLS_UNICAST);
529 selector.matchEthDst(ethCriterion.mac());
530 treatment.transition(MPLS_TABLE_0);
531 rule = DefaultFlowRule.builder()
532 .forDevice(deviceId)
533 .withSelector(selector.build())
534 .withTreatment(treatment.build())
535 .withPriority(DEFAULT_PRIORITY)
536 .fromApp(applicationId)
537 .makePermanent()
538 .forTable(TMAC_TABLE).build();
539 rules.add(rule);
540 }
541 return rules;
542 }
543
544 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
545 switch (fwd.flag()) {
546 case SPECIFIC:
547 return processSpecific(fwd);
548 case VERSATILE:
549 return processVersatile(fwd);
550 default:
551 fail(fwd, ObjectiveError.UNKNOWN);
552 log.warn("Unknown forwarding flag {}", fwd.flag());
553 }
554 return Collections.emptySet();
555 }
556
557 /**
558 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
559 * ACL table.
560 * @param fwd the forwarding objective of type 'versatile'
561 * @return a collection of flow rules to be sent to the switch. An empty
562 * collection may be returned if there is a problem in processing
563 * the flow rule
564 */
565 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
566 log.info("Processing versatile forwarding objective");
567 TrafficSelector selector = fwd.selector();
568
569 EthTypeCriterion ethType =
570 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
571 if (ethType == null) {
572 log.error("Versatile forwarding objective must include ethType");
573 fail(fwd, ObjectiveError.BADPARAMS);
574 return Collections.emptySet();
575 }
576 if (fwd.nextId() == null && fwd.treatment() == null) {
577 log.error("Forwarding objective {} from {} must contain "
578 + "nextId or Treatment", fwd.selector(), fwd.appId());
579 return Collections.emptySet();
580 }
581 // XXX driver does not currently do type checking as per Tables 65-67 in
582 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
583 if (fwd.treatment() != null &&
584 fwd.treatment().allInstructions().size() == 1 &&
585 fwd.treatment().allInstructions().get(0).type() == Instruction.Type.OUTPUT) {
586 OutputInstruction o = (OutputInstruction) fwd.treatment().allInstructions().get(0);
587 if (o.port() == PortNumber.CONTROLLER) {
588 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
589 .fromApp(fwd.appId())
590 .withPriority(fwd.priority())
591 .forDevice(deviceId)
592 .withSelector(fwd.selector())
593 .withTreatment(fwd.treatment())
594 .makePermanent()
595 .forTable(ACL_TABLE);
596 return Collections.singletonList(ruleBuilder.build());
597 } else {
598 log.warn("Only allowed treatments in versatile forwarding "
599 + "objectives are punts to the controller");
600 return Collections.emptySet();
601 }
602 }
603
604 if (fwd.nextId() != null) {
605 // XXX overide case
606 log.warn("versatile objective --> next Id not yet implemeted");
607 }
608 return Collections.emptySet();
609 }
610
611 /**
612 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800613 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700614 *
615 * @param fwd the forwarding objective of type 'specific'
616 * @return a collection of flow rules. Typically there will be only one
617 * for this type of forwarding objective. An empty set may be
618 * returned if there is an issue in processing the objective.
619 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800620 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das4ce45962015-11-24 23:21:05 -0800621 log.trace("Processing specific fwd objective:{} in dev:{} with next:{}",
622 fwd.id(), deviceId, fwd.nextId());
623 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
624 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
625
626 if (isEthTypeObj) {
627 return processEthTypeSpecific(fwd);
628 } else if (isEthDstObj) {
629 return processEthDstSpecific(fwd);
630 } else {
631 log.warn("processSpecific: Unsupported forwarding objective "
632 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700633 fail(fwd, ObjectiveError.UNSUPPORTED);
634 return Collections.emptySet();
635 }
Saurav Das4ce45962015-11-24 23:21:05 -0800636 }
637
638 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
639 TrafficSelector selector = fwd.selector();
640 EthTypeCriterion ethType = (EthTypeCriterion) selector
641 .getCriterion(Criterion.Type.ETH_TYPE);
Charles Chan188ebf52015-12-23 00:15:11 -0800642 return !((ethType == null) ||
Saurav Das4ce45962015-11-24 23:21:05 -0800643 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Charles Chan188ebf52015-12-23 00:15:11 -0800644 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
Saurav Das4ce45962015-11-24 23:21:05 -0800645 }
646
647 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
648 TrafficSelector selector = fwd.selector();
649 EthCriterion ethDst = (EthCriterion) selector
650 .getCriterion(Criterion.Type.ETH_DST);
651 VlanIdCriterion vlanId = (VlanIdCriterion) selector
652 .getCriterion(Criterion.Type.VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800653 return !(ethDst == null && vlanId == null);
Saurav Das4ce45962015-11-24 23:21:05 -0800654 }
655
656 /**
657 * Handles forwarding rules to the IP and MPLS tables.
658 *
659 * @param fwd the forwarding objective
660 * @return A collection of flow rules, or an empty set
661 */
662 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
663 TrafficSelector selector = fwd.selector();
664 EthTypeCriterion ethType =
665 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700666
Charles Chan188ebf52015-12-23 00:15:11 -0800667 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800668 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
669 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
670 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
671 .matchIPDst(((IPCriterion)
672 selector.getCriterion(Criterion.Type.IPV4_DST)).ip());
673 forTableId = UNICAST_ROUTING_TABLE;
Saurav Das4ce45962015-11-24 23:21:05 -0800674 log.debug("processing IPv4 specific forwarding objective {} -> next:{}"
675 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800676 } else {
677 filteredSelector
678 .matchEthType(Ethernet.MPLS_UNICAST)
679 .matchMplsLabel(((MplsCriterion)
680 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
681 MplsBosCriterion bos = (MplsBosCriterion) selector
682 .getCriterion(Criterion.Type.MPLS_BOS);
683 if (bos != null) {
684 filteredSelector.matchMplsBos(bos.mplsBos());
685 }
686 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800687 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
688 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800689 }
Saurav Das822c4e22015-10-23 10:51:11 -0700690
691 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Saurav Das8a0732e2015-11-20 15:27:53 -0800692 boolean popMpls = false;
693 if (fwd.treatment() != null) {
694 for (Instruction i : fwd.treatment().allInstructions()) {
Charles Chan7d10b162015-12-07 18:54:45 -0800695 /*
696 * NOTE: OF-DPA does not support immediate instruction in
697 * L3 unicast and MPLS table.
698 */
699 tb.deferred().add(i);
Saurav Das8a0732e2015-11-20 15:27:53 -0800700 if (i instanceof L2ModificationInstruction &&
701 ((L2ModificationInstruction) i).subtype() == L2SubType.MPLS_POP) {
702 popMpls = true;
703 }
704 }
705 }
Saurav Das822c4e22015-10-23 10:51:11 -0700706
707 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800708 if (forTableId == MPLS_TABLE_1 && !popMpls) {
709 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
710 + "is not implemented in OF-DPA yet. Aborting this flow "
711 + "in this device {}", deviceId);
712 // XXX We could convert to forwarding to a single-port, via a
713 // MPLS interface, or a MPLS SWAP (with-same) but that would
714 // have to be handled in the next-objective. Also the pop-mpls
715 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800716 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800717 return Collections.emptySet();
718 }
719
Saurav Das423fe2b2015-12-04 10:52:59 -0800720 NextGroup next = getGroupForNextObjective(fwd.nextId());
721 if (next != null) {
722 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
723 // we only need the top level group's key to point the flow to it
724 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
725 if (group == null) {
726 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
727 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
728 fail(fwd, ObjectiveError.GROUPMISSING);
729 return Collections.emptySet();
730 }
731 tb.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700732 }
Saurav Das822c4e22015-10-23 10:51:11 -0700733 }
734 tb.transition(ACL_TABLE);
735 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
736 .fromApp(fwd.appId())
737 .withPriority(fwd.priority())
738 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800739 .withSelector(filteredSelector.build())
740 .withTreatment(tb.build())
741 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700742
743 if (fwd.permanent()) {
744 ruleBuilder.makePermanent();
745 } else {
746 ruleBuilder.makeTemporary(fwd.timeout());
747 }
748
Saurav Das822c4e22015-10-23 10:51:11 -0700749 return Collections.singletonList(ruleBuilder.build());
750 }
751
Saurav Das4ce45962015-11-24 23:21:05 -0800752 /**
753 * Handles forwarding rules to the L2 bridging table. Flow actions are not
754 * allowed in the bridging table - instead we use L2 Interface group or
755 * L2 flood group
756 *
757 * @param fwd the forwarding objective
758 * @return A collection of flow rules, or an empty set
759 */
760 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
761 List<FlowRule> rules = new ArrayList<>();
762
763 // Build filtered selector
764 TrafficSelector selector = fwd.selector();
765 EthCriterion ethCriterion = (EthCriterion) selector
766 .getCriterion(Criterion.Type.ETH_DST);
767 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
768 .getCriterion(Criterion.Type.VLAN_VID);
769
770 if (vlanIdCriterion == null) {
771 log.warn("Forwarding objective for bridging requires vlan. Not "
772 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
773 fail(fwd, ObjectiveError.BADPARAMS);
774 return Collections.emptySet();
775 }
776
777 TrafficSelector.Builder filteredSelectorBuilder =
778 DefaultTrafficSelector.builder();
779 // Do not match MacAddress for subnet broadcast entry
780 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
781 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
782 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
783 fwd.id(), fwd.nextId(), deviceId);
784 } else {
785 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
786 + "in dev:{} for vlan:{}",
787 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
788 }
789 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
790 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
791
792 if (fwd.treatment() != null) {
793 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
794 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
795 }
796
797 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
798 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800799 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -0800800 if (next != null) {
801 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
802 // we only need the top level group's key to point the flow to it
803 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
804 if (group != null) {
805 treatmentBuilder.deferred().group(group.id());
806 } else {
807 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
808 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
809 fail(fwd, ObjectiveError.GROUPMISSING);
810 return Collections.emptySet();
811 }
812 }
813 }
814 treatmentBuilder.immediate().transition(ACL_TABLE);
815 TrafficTreatment filteredTreatment = treatmentBuilder.build();
816
817 // Build bridging table entries
818 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
819 flowRuleBuilder.fromApp(fwd.appId())
820 .withPriority(fwd.priority())
821 .forDevice(deviceId)
822 .withSelector(filteredSelector)
823 .withTreatment(filteredTreatment)
824 .forTable(BRIDGING_TABLE);
825 if (fwd.permanent()) {
826 flowRuleBuilder.makePermanent();
827 } else {
828 flowRuleBuilder.makeTemporary(fwd.timeout());
829 }
830 rules.add(flowRuleBuilder.build());
831 return rules;
832 }
833
Saurav Das423fe2b2015-12-04 10:52:59 -0800834 protected NextGroup getGroupForNextObjective(Integer nextId) {
835 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
836 if (next != null) {
837 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
838 if (gkeys != null && !gkeys.isEmpty()) {
839 return next;
840 } else {
841 log.warn("Empty next group found in FlowObjective store for "
842 + "next-id:{} in dev:{}", nextId, deviceId);
843 }
844 } else {
845 log.warn("next-id {} not found in Flow objective store for dev:{}",
846 nextId, deviceId);
847 }
848 return null;
849 }
850
Charles Chan188ebf52015-12-23 00:15:11 -0800851 protected static void pass(Objective obj) {
Saurav Das822c4e22015-10-23 10:51:11 -0700852 if (obj.context().isPresent()) {
853 obj.context().get().onSuccess(obj);
854 }
855 }
856
Charles Chan188ebf52015-12-23 00:15:11 -0800857 protected static void fail(Objective obj, ObjectiveError error) {
Saurav Das822c4e22015-10-23 10:51:11 -0700858 if (obj.context().isPresent()) {
859 obj.context().get().onError(obj, error);
860 }
861 }
Saurav Das822c4e22015-10-23 10:51:11 -0700862}