blob: 1cfb9371c3bd8b5ec14074355c33e000230d5535 [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;
Sho SHIMIZU45906042016-01-13 23:05:54 -080026import java.util.Objects;
Saurav Das822c4e22015-10-23 10:51:11 -070027import java.util.Set;
28import java.util.concurrent.ConcurrentHashMap;
Saurav Das822c4e22015-10-23 10:51:11 -070029
30import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070031import org.onlab.packet.Ethernet;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070033import org.onlab.packet.VlanId;
34import org.onlab.util.KryoNamespace;
35import org.onosproject.core.ApplicationId;
36import org.onosproject.core.CoreService;
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)
Andrea Campanella238d96e2016-01-20 11:52:02 -0800115 .register(OFDPA2GroupHandler.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()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800181 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700182 .forEach(flowOpsBuilder::add);
183 break;
184 case REMOVE:
185 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800186 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700187 .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;
Charles Chane849c192016-01-11 18:28:54 -0800307 // For VLAN cross-connect packets, use the configured VLAN
308 if (vidCriterion != null) {
309 if (vidCriterion.vlanId() != VlanId.NONE) {
310 assignedVlan = vidCriterion.vlanId();
311
312 // For untagged packets, assign a VLAN ID
313 } else {
314 if (filt.meta() == null) {
315 log.error("Missing metadata in filtering objective required " +
316 "for vlan assignment in dev {}", deviceId);
317 fail(filt, ObjectiveError.BADPARAMS);
318 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700319 }
Charles Chane849c192016-01-11 18:28:54 -0800320 for (Instruction i : filt.meta().allInstructions()) {
321 if (i instanceof ModVlanIdInstruction) {
322 assignedVlan = ((ModVlanIdInstruction) i).vlanId();
323 }
324 }
325 if (assignedVlan == null) {
326 log.error("Driver requires an assigned vlan-id to tag incoming "
327 + "untagged packets. Not processing vlan filters on "
328 + "device {}", deviceId);
329 fail(filt, ObjectiveError.BADPARAMS);
330 return;
331 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700332 }
333 }
334
Charles Chane849c192016-01-11 18:28:54 -0800335 if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700336 log.debug("filtering objective missing dstMac, cannot program TMAC table");
337 } else {
338 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700339 vidCriterion, assignedVlan,
340 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700341 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
342 tmacRule, deviceId);
343 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
344 }
345 }
346
347 if (ethCriterion == null || vidCriterion == null) {
Charles Chane849c192016-01-11 18:28:54 -0800348 log.debug("filtering objective missing dstMac or VLAN, "
349 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700350 } else {
351 for (FlowRule vlanRule : processVlanIdFilter(portCriterion, vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700352 assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700353 applicationId)) {
354 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
355 vlanRule, deviceId);
356 ops = install ? ops.add(vlanRule) : ops.remove(vlanRule);
357 }
358 }
359
360 for (IPCriterion ipaddr : ips) {
361 // since we ignore port information for IP rules, and the same (gateway) IP
362 // can be configured on multiple ports, we make sure that we send
363 // only a single rule to the switch.
364 if (!sentIpFilters.contains(ipaddr)) {
365 sentIpFilters.add(ipaddr);
366 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
367 ipaddr, deviceId);
368 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
369 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
370 selector.matchEthType(Ethernet.TYPE_IPV4);
371 selector.matchIPDst(ipaddr.ip());
372 treatment.setOutput(PortNumber.CONTROLLER);
373 FlowRule rule = DefaultFlowRule.builder()
374 .forDevice(deviceId)
375 .withSelector(selector.build())
376 .withTreatment(treatment.build())
377 .withPriority(HIGHEST_PRIORITY)
378 .fromApp(applicationId)
379 .makePermanent()
380 .forTable(ACL_TABLE).build();
381 ops = install ? ops.add(rule) : ops.remove(rule);
382 }
383 }
384
385 // apply filtering flow rules
386 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
387 @Override
388 public void onSuccess(FlowRuleOperations ops) {
389 log.info("Applied {} filtering rules in device {}",
390 ops.stages().get(0).size(), deviceId);
391 pass(filt);
392 }
393
394 @Override
395 public void onError(FlowRuleOperations ops) {
396 log.info("Failed to apply all filtering rules in dev {}", deviceId);
397 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
398 }
399 }));
400
401 }
402
403 /**
404 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700405 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700406 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700407 *
Saurav Das822c4e22015-10-23 10:51:11 -0700408 * @param portCriterion port on device for which this filter is programmed
409 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700410 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700411 * @param applicationId for application programming this filter
412 * @return list of FlowRule for port-vlan filters
413 */
414 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
415 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700416 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700417 ApplicationId applicationId) {
418 List<FlowRule> rules = new ArrayList<FlowRule>();
419 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
420 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
421 selector.matchVlanId(vidCriterion.vlanId());
Saurav Das4f980082015-11-05 13:39:15 -0800422 treatment.transition(TMAC_TABLE);
423
424 VlanId storeVlan = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700425 if (vidCriterion.vlanId() == VlanId.NONE) {
426 // untagged packets are assigned vlans
Saurav Das0e99e2b2015-10-28 12:39:42 -0700427 treatment.pushVlan().setVlanId(assignedVlan);
Saurav Das2857f382015-11-03 14:39:27 -0800428 // XXX ofdpa will require an additional vlan match on the assigned vlan
429 // and it may not require the push. This is not in compliance with OF
430 // standard. Waiting on what the exact flows are going to look like.
Saurav Das4f980082015-11-05 13:39:15 -0800431 storeVlan = assignedVlan;
432 } else {
433 storeVlan = vidCriterion.vlanId();
Saurav Das822c4e22015-10-23 10:51:11 -0700434 }
Saurav Das822c4e22015-10-23 10:51:11 -0700435
436 // ofdpa cannot match on ALL portnumber, so we need to use separate
437 // rules for each port.
438 List<PortNumber> portnums = new ArrayList<PortNumber>();
439 if (portCriterion.port() == PortNumber.ALL) {
440 for (Port port : deviceService.getPorts(deviceId)) {
441 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
442 portnums.add(port.number());
443 }
444 }
445 } else {
446 portnums.add(portCriterion.port());
447 }
Saurav Das4f980082015-11-05 13:39:15 -0800448
Saurav Das822c4e22015-10-23 10:51:11 -0700449 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800450 // update storage
Charles Chan188ebf52015-12-23 00:15:11 -0800451 ofdpa2GroupHandler.port2Vlan.put(pnum, storeVlan);
452 Set<PortNumber> vlanPorts = ofdpa2GroupHandler.vlan2Port.get(storeVlan);
Saurav Das4f980082015-11-05 13:39:15 -0800453 if (vlanPorts == null) {
454 vlanPorts = Collections.newSetFromMap(
455 new ConcurrentHashMap<PortNumber, Boolean>());
456 vlanPorts.add(pnum);
Charles Chan188ebf52015-12-23 00:15:11 -0800457 ofdpa2GroupHandler.vlan2Port.put(storeVlan, vlanPorts);
Saurav Das4f980082015-11-05 13:39:15 -0800458 } else {
459 vlanPorts.add(pnum);
460 }
461 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700462 selector.matchInPort(pnum);
463 FlowRule rule = DefaultFlowRule.builder()
464 .forDevice(deviceId)
465 .withSelector(selector.build())
466 .withTreatment(treatment.build())
467 .withPriority(DEFAULT_PRIORITY)
468 .fromApp(applicationId)
469 .makePermanent()
470 .forTable(VLAN_TABLE).build();
471 rules.add(rule);
472 }
473 return rules;
474 }
475
476 /**
477 * Allows routed packets with correct destination MAC to be directed
478 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700479 *
480 * @param portCriterion port on device for which this filter is programmed
481 * @param ethCriterion dstMac of device for which is filter is programmed
482 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700483 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700484 * @param applicationId for application programming this filter
485 * @return list of FlowRule for port-vlan filters
486
487 */
488 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
489 EthCriterion ethCriterion,
490 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700491 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700492 ApplicationId applicationId) {
493 //handling untagged packets via assigned VLAN
494 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700495 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700496 }
497 // ofdpa cannot match on ALL portnumber, so we need to use separate
498 // rules for each port.
499 List<PortNumber> portnums = new ArrayList<PortNumber>();
500 if (portCriterion.port() == PortNumber.ALL) {
501 for (Port port : deviceService.getPorts(deviceId)) {
502 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
503 portnums.add(port.number());
504 }
505 }
506 } else {
507 portnums.add(portCriterion.port());
508 }
509
510 List<FlowRule> rules = new ArrayList<FlowRule>();
511 for (PortNumber pnum : portnums) {
512 // for unicast IP packets
513 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
514 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
515 selector.matchInPort(pnum);
516 selector.matchVlanId(vidCriterion.vlanId());
517 selector.matchEthType(Ethernet.TYPE_IPV4);
518 selector.matchEthDst(ethCriterion.mac());
519 treatment.transition(UNICAST_ROUTING_TABLE);
520 FlowRule rule = DefaultFlowRule.builder()
521 .forDevice(deviceId)
522 .withSelector(selector.build())
523 .withTreatment(treatment.build())
524 .withPriority(DEFAULT_PRIORITY)
525 .fromApp(applicationId)
526 .makePermanent()
527 .forTable(TMAC_TABLE).build();
528 rules.add(rule);
529 //for MPLS packets
530 selector = DefaultTrafficSelector.builder();
531 treatment = DefaultTrafficTreatment.builder();
532 selector.matchInPort(pnum);
533 selector.matchVlanId(vidCriterion.vlanId());
534 selector.matchEthType(Ethernet.MPLS_UNICAST);
535 selector.matchEthDst(ethCriterion.mac());
536 treatment.transition(MPLS_TABLE_0);
537 rule = DefaultFlowRule.builder()
538 .forDevice(deviceId)
539 .withSelector(selector.build())
540 .withTreatment(treatment.build())
541 .withPriority(DEFAULT_PRIORITY)
542 .fromApp(applicationId)
543 .makePermanent()
544 .forTable(TMAC_TABLE).build();
545 rules.add(rule);
546 }
547 return rules;
548 }
549
550 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
551 switch (fwd.flag()) {
552 case SPECIFIC:
553 return processSpecific(fwd);
554 case VERSATILE:
555 return processVersatile(fwd);
556 default:
557 fail(fwd, ObjectiveError.UNKNOWN);
558 log.warn("Unknown forwarding flag {}", fwd.flag());
559 }
560 return Collections.emptySet();
561 }
562
563 /**
564 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
565 * ACL table.
566 * @param fwd the forwarding objective of type 'versatile'
567 * @return a collection of flow rules to be sent to the switch. An empty
568 * collection may be returned if there is a problem in processing
569 * the flow rule
570 */
571 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
572 log.info("Processing versatile forwarding objective");
573 TrafficSelector selector = fwd.selector();
574
575 EthTypeCriterion ethType =
576 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
577 if (ethType == null) {
578 log.error("Versatile forwarding objective must include ethType");
579 fail(fwd, ObjectiveError.BADPARAMS);
580 return Collections.emptySet();
581 }
582 if (fwd.nextId() == null && fwd.treatment() == null) {
583 log.error("Forwarding objective {} from {} must contain "
584 + "nextId or Treatment", fwd.selector(), fwd.appId());
585 return Collections.emptySet();
586 }
587 // XXX driver does not currently do type checking as per Tables 65-67 in
588 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
589 if (fwd.treatment() != null &&
590 fwd.treatment().allInstructions().size() == 1 &&
591 fwd.treatment().allInstructions().get(0).type() == Instruction.Type.OUTPUT) {
592 OutputInstruction o = (OutputInstruction) fwd.treatment().allInstructions().get(0);
593 if (o.port() == PortNumber.CONTROLLER) {
594 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
595 .fromApp(fwd.appId())
596 .withPriority(fwd.priority())
597 .forDevice(deviceId)
598 .withSelector(fwd.selector())
599 .withTreatment(fwd.treatment())
600 .makePermanent()
601 .forTable(ACL_TABLE);
602 return Collections.singletonList(ruleBuilder.build());
603 } else {
604 log.warn("Only allowed treatments in versatile forwarding "
605 + "objectives are punts to the controller");
606 return Collections.emptySet();
607 }
608 }
609
610 if (fwd.nextId() != null) {
611 // XXX overide case
612 log.warn("versatile objective --> next Id not yet implemeted");
613 }
614 return Collections.emptySet();
615 }
616
617 /**
618 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800619 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700620 *
621 * @param fwd the forwarding objective of type 'specific'
622 * @return a collection of flow rules. Typically there will be only one
623 * for this type of forwarding objective. An empty set may be
624 * returned if there is an issue in processing the objective.
625 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800626 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das4ce45962015-11-24 23:21:05 -0800627 log.trace("Processing specific fwd objective:{} in dev:{} with next:{}",
628 fwd.id(), deviceId, fwd.nextId());
629 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
630 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
631
632 if (isEthTypeObj) {
633 return processEthTypeSpecific(fwd);
634 } else if (isEthDstObj) {
635 return processEthDstSpecific(fwd);
636 } else {
637 log.warn("processSpecific: Unsupported forwarding objective "
638 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700639 fail(fwd, ObjectiveError.UNSUPPORTED);
640 return Collections.emptySet();
641 }
Saurav Das4ce45962015-11-24 23:21:05 -0800642 }
643
644 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
645 TrafficSelector selector = fwd.selector();
646 EthTypeCriterion ethType = (EthTypeCriterion) selector
647 .getCriterion(Criterion.Type.ETH_TYPE);
Charles Chan188ebf52015-12-23 00:15:11 -0800648 return !((ethType == null) ||
Saurav Das4ce45962015-11-24 23:21:05 -0800649 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Charles Chan188ebf52015-12-23 00:15:11 -0800650 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
Saurav Das4ce45962015-11-24 23:21:05 -0800651 }
652
653 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
654 TrafficSelector selector = fwd.selector();
655 EthCriterion ethDst = (EthCriterion) selector
656 .getCriterion(Criterion.Type.ETH_DST);
657 VlanIdCriterion vlanId = (VlanIdCriterion) selector
658 .getCriterion(Criterion.Type.VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800659 return !(ethDst == null && vlanId == null);
Saurav Das4ce45962015-11-24 23:21:05 -0800660 }
661
662 /**
663 * Handles forwarding rules to the IP and MPLS tables.
664 *
665 * @param fwd the forwarding objective
666 * @return A collection of flow rules, or an empty set
667 */
668 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
669 TrafficSelector selector = fwd.selector();
670 EthTypeCriterion ethType =
671 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700672
Charles Chan188ebf52015-12-23 00:15:11 -0800673 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800674 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
675 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
676 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
677 .matchIPDst(((IPCriterion)
678 selector.getCriterion(Criterion.Type.IPV4_DST)).ip());
679 forTableId = UNICAST_ROUTING_TABLE;
Saurav Das4ce45962015-11-24 23:21:05 -0800680 log.debug("processing IPv4 specific forwarding objective {} -> next:{}"
681 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800682 } else {
683 filteredSelector
684 .matchEthType(Ethernet.MPLS_UNICAST)
685 .matchMplsLabel(((MplsCriterion)
686 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
687 MplsBosCriterion bos = (MplsBosCriterion) selector
688 .getCriterion(Criterion.Type.MPLS_BOS);
689 if (bos != null) {
690 filteredSelector.matchMplsBos(bos.mplsBos());
691 }
692 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800693 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
694 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800695 }
Saurav Das822c4e22015-10-23 10:51:11 -0700696
697 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Saurav Das8a0732e2015-11-20 15:27:53 -0800698 boolean popMpls = false;
699 if (fwd.treatment() != null) {
700 for (Instruction i : fwd.treatment().allInstructions()) {
Charles Chan7d10b162015-12-07 18:54:45 -0800701 /*
702 * NOTE: OF-DPA does not support immediate instruction in
703 * L3 unicast and MPLS table.
704 */
705 tb.deferred().add(i);
Saurav Das8a0732e2015-11-20 15:27:53 -0800706 if (i instanceof L2ModificationInstruction &&
707 ((L2ModificationInstruction) i).subtype() == L2SubType.MPLS_POP) {
708 popMpls = true;
709 }
710 }
711 }
Saurav Das822c4e22015-10-23 10:51:11 -0700712
713 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800714 if (forTableId == MPLS_TABLE_1 && !popMpls) {
715 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
716 + "is not implemented in OF-DPA yet. Aborting this flow "
717 + "in this device {}", deviceId);
718 // XXX We could convert to forwarding to a single-port, via a
719 // MPLS interface, or a MPLS SWAP (with-same) but that would
720 // have to be handled in the next-objective. Also the pop-mpls
721 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800722 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800723 return Collections.emptySet();
724 }
725
Saurav Das423fe2b2015-12-04 10:52:59 -0800726 NextGroup next = getGroupForNextObjective(fwd.nextId());
727 if (next != null) {
728 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
729 // we only need the top level group's key to point the flow to it
730 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
731 if (group == null) {
732 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
733 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
734 fail(fwd, ObjectiveError.GROUPMISSING);
735 return Collections.emptySet();
736 }
737 tb.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700738 }
Saurav Das822c4e22015-10-23 10:51:11 -0700739 }
740 tb.transition(ACL_TABLE);
741 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
742 .fromApp(fwd.appId())
743 .withPriority(fwd.priority())
744 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800745 .withSelector(filteredSelector.build())
746 .withTreatment(tb.build())
747 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700748
749 if (fwd.permanent()) {
750 ruleBuilder.makePermanent();
751 } else {
752 ruleBuilder.makeTemporary(fwd.timeout());
753 }
754
Saurav Das822c4e22015-10-23 10:51:11 -0700755 return Collections.singletonList(ruleBuilder.build());
756 }
757
Saurav Das4ce45962015-11-24 23:21:05 -0800758 /**
759 * Handles forwarding rules to the L2 bridging table. Flow actions are not
760 * allowed in the bridging table - instead we use L2 Interface group or
761 * L2 flood group
762 *
763 * @param fwd the forwarding objective
764 * @return A collection of flow rules, or an empty set
765 */
766 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
767 List<FlowRule> rules = new ArrayList<>();
768
769 // Build filtered selector
770 TrafficSelector selector = fwd.selector();
771 EthCriterion ethCriterion = (EthCriterion) selector
772 .getCriterion(Criterion.Type.ETH_DST);
773 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
774 .getCriterion(Criterion.Type.VLAN_VID);
775
776 if (vlanIdCriterion == null) {
777 log.warn("Forwarding objective for bridging requires vlan. Not "
778 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
779 fail(fwd, ObjectiveError.BADPARAMS);
780 return Collections.emptySet();
781 }
782
783 TrafficSelector.Builder filteredSelectorBuilder =
784 DefaultTrafficSelector.builder();
785 // Do not match MacAddress for subnet broadcast entry
786 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
787 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
788 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
789 fwd.id(), fwd.nextId(), deviceId);
790 } else {
791 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
792 + "in dev:{} for vlan:{}",
793 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
794 }
795 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
796 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
797
798 if (fwd.treatment() != null) {
799 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
800 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
801 }
802
803 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
804 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800805 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -0800806 if (next != null) {
807 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
808 // we only need the top level group's key to point the flow to it
809 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
810 if (group != null) {
811 treatmentBuilder.deferred().group(group.id());
812 } else {
813 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
814 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
815 fail(fwd, ObjectiveError.GROUPMISSING);
816 return Collections.emptySet();
817 }
818 }
819 }
820 treatmentBuilder.immediate().transition(ACL_TABLE);
821 TrafficTreatment filteredTreatment = treatmentBuilder.build();
822
823 // Build bridging table entries
824 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
825 flowRuleBuilder.fromApp(fwd.appId())
826 .withPriority(fwd.priority())
827 .forDevice(deviceId)
828 .withSelector(filteredSelector)
829 .withTreatment(filteredTreatment)
830 .forTable(BRIDGING_TABLE);
831 if (fwd.permanent()) {
832 flowRuleBuilder.makePermanent();
833 } else {
834 flowRuleBuilder.makeTemporary(fwd.timeout());
835 }
836 rules.add(flowRuleBuilder.build());
837 return rules;
838 }
839
Saurav Das423fe2b2015-12-04 10:52:59 -0800840 protected NextGroup getGroupForNextObjective(Integer nextId) {
841 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
842 if (next != null) {
843 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
844 if (gkeys != null && !gkeys.isEmpty()) {
845 return next;
846 } else {
847 log.warn("Empty next group found in FlowObjective store for "
848 + "next-id:{} in dev:{}", nextId, deviceId);
849 }
850 } else {
851 log.warn("next-id {} not found in Flow objective store for dev:{}",
852 nextId, deviceId);
853 }
854 return null;
855 }
856
Charles Chan188ebf52015-12-23 00:15:11 -0800857 protected static void pass(Objective obj) {
Saurav Das822c4e22015-10-23 10:51:11 -0700858 if (obj.context().isPresent()) {
859 obj.context().get().onSuccess(obj);
860 }
861 }
862
Charles Chan188ebf52015-12-23 00:15:11 -0800863 protected static void fail(Objective obj, ObjectiveError error) {
Saurav Das822c4e22015-10-23 10:51:11 -0700864 if (obj.context().isPresent()) {
865 obj.context().get().onError(obj, error);
866 }
867 }
Saurav Das822c4e22015-10-23 10:51:11 -0700868}