blob: cce974175ea788b8adf4b9736c8040baf9f1b11d [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;
Charles Chan188ebf52015-12-23 00:15:11 -080037import org.onosproject.driver.pipeline.OFDPA2GroupHandler.OfdpaNextGroup;
Saurav Das822c4e22015-10-23 10:51:11 -070038import org.onosproject.net.DeviceId;
39import org.onosproject.net.Port;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.behaviour.NextGroup;
42import org.onosproject.net.behaviour.Pipeliner;
43import org.onosproject.net.behaviour.PipelinerContext;
44import org.onosproject.net.device.DeviceService;
45import org.onosproject.net.driver.AbstractHandlerBehaviour;
46import org.onosproject.net.flow.DefaultFlowRule;
47import org.onosproject.net.flow.DefaultTrafficSelector;
48import org.onosproject.net.flow.DefaultTrafficTreatment;
49import org.onosproject.net.flow.FlowRule;
50import org.onosproject.net.flow.FlowRuleOperations;
51import org.onosproject.net.flow.FlowRuleOperationsContext;
52import org.onosproject.net.flow.FlowRuleService;
53import org.onosproject.net.flow.TrafficSelector;
54import org.onosproject.net.flow.TrafficTreatment;
55import org.onosproject.net.flow.criteria.Criteria;
56import org.onosproject.net.flow.criteria.Criterion;
57import org.onosproject.net.flow.criteria.EthCriterion;
58import org.onosproject.net.flow.criteria.EthTypeCriterion;
59import org.onosproject.net.flow.criteria.IPCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080060import org.onosproject.net.flow.criteria.MplsBosCriterion;
61import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070062import org.onosproject.net.flow.criteria.PortCriterion;
63import org.onosproject.net.flow.criteria.VlanIdCriterion;
64import org.onosproject.net.flow.instructions.Instruction;
65import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
66import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080067import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
69import org.onosproject.net.flowobjective.FilteringObjective;
70import org.onosproject.net.flowobjective.FlowObjectiveStore;
71import org.onosproject.net.flowobjective.ForwardingObjective;
72import org.onosproject.net.flowobjective.NextObjective;
73import org.onosproject.net.flowobjective.Objective;
74import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.group.DefaultGroupKey;
76import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070078import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070079import org.onosproject.store.serializers.KryoNamespaces;
80import org.slf4j.Logger;
81
Saurav Das822c4e22015-10-23 10:51:11 -070082/**
83 * Driver for Broadcom's OF-DPA v2.0 TTP.
84 *
85 */
86public class OFDPA2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -070087 protected static final int PORT_TABLE = 0;
88 protected static final int VLAN_TABLE = 10;
89 protected static final int TMAC_TABLE = 20;
90 protected static final int UNICAST_ROUTING_TABLE = 30;
91 protected static final int MULTICAST_ROUTING_TABLE = 40;
92 protected static final int MPLS_TABLE_0 = 23;
93 protected static final int MPLS_TABLE_1 = 24;
94 protected static final int BRIDGING_TABLE = 50;
95 protected static final int ACL_TABLE = 60;
96 protected static final int MAC_LEARNING_TABLE = 254;
97 protected static final long OFPP_MAX = 0xffffff00L;
98
99 private static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800100 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700101 protected static final int LOWEST_PRIORITY = 0x0;
102
Saurav Das822c4e22015-10-23 10:51:11 -0700103 private final Logger log = getLogger(getClass());
104 private ServiceDirectory serviceDirectory;
105 protected FlowRuleService flowRuleService;
106 private CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800107 protected GroupService groupService;
108 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700109 protected DeviceId deviceId;
110 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700111 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800112 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700113 .register(KryoNamespaces.API)
114 .register(GroupKey.class)
115 .register(DefaultGroupKey.class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800116 .register(OfdpaNextGroup.class)
Saurav Das822c4e22015-10-23 10:51:11 -0700117 .register(byte[].class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800118 .register(ArrayDeque.class)
Saurav Das822c4e22015-10-23 10:51:11 -0700119 .build();
120
Charles Chan188ebf52015-12-23 00:15:11 -0800121 protected OFDPA2GroupHandler ofdpa2GroupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700122
Saurav Das822c4e22015-10-23 10:51:11 -0700123 private Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800124 new ConcurrentHashMap<>());
Saurav Das4f980082015-11-05 13:39:15 -0800125
Saurav Das822c4e22015-10-23 10:51:11 -0700126 @Override
127 public void init(DeviceId deviceId, PipelinerContext context) {
128 this.serviceDirectory = context.directory();
129 this.deviceId = deviceId;
130
Charles Chan188ebf52015-12-23 00:15:11 -0800131 // Initialize OFDPA group handler
132 ofdpa2GroupHandler = new OFDPA2GroupHandler();
133 ofdpa2GroupHandler.init(deviceId, context);
Saurav Das822c4e22015-10-23 10:51:11 -0700134
135 coreService = serviceDirectory.get(CoreService.class);
136 flowRuleService = serviceDirectory.get(FlowRuleService.class);
137 groupService = serviceDirectory.get(GroupService.class);
138 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700139 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700140
141 driverId = coreService.registerApplication(
142 "org.onosproject.driver.OFDPA2Pipeline");
143
Saurav Das822c4e22015-10-23 10:51:11 -0700144 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700145 }
146
147 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800148 // OF-DPA does not require initializing the pipeline as it puts default
149 // rules automatically in the hardware. However emulation of OFDPA in
150 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700151 }
152
153 //////////////////////////////////////
154 // Flow Objectives
155 //////////////////////////////////////
156
157 @Override
158 public void filter(FilteringObjective filteringObjective) {
159 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
160 processFilter(filteringObjective,
161 filteringObjective.op() == Objective.Operation.ADD,
162 filteringObjective.appId());
163 } else {
164 // Note that packets that don't match the PERMIT filter are
165 // automatically denied. The DENY filter is used to deny packets
166 // that are otherwise permitted by the PERMIT filter.
167 // Use ACL table flow rules here for DENY filtering objectives
168 log.debug("filter objective other than PERMIT currently not supported");
169 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
170 }
171 }
172
173 @Override
174 public void forward(ForwardingObjective fwd) {
175 Collection<FlowRule> rules;
176 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
177
178 rules = processForward(fwd);
179 switch (fwd.op()) {
180 case ADD:
181 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800182 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700183 .forEach(flowOpsBuilder::add);
184 break;
185 case REMOVE:
186 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800187 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700188 .forEach(flowOpsBuilder::remove);
189 break;
190 default:
191 fail(fwd, ObjectiveError.UNKNOWN);
192 log.warn("Unknown forwarding type {}", fwd.op());
193 }
194
Saurav Das822c4e22015-10-23 10:51:11 -0700195 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
196 @Override
197 public void onSuccess(FlowRuleOperations ops) {
198 pass(fwd);
199 }
200
201 @Override
202 public void onError(FlowRuleOperations ops) {
203 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
204 }
205 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700206 }
207
208 @Override
209 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800210 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
211 switch (nextObjective.op()) {
212 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800213 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800214 log.warn("Cannot add next {} that already exists in device {}",
215 nextObjective.id(), deviceId);
216 return;
217 }
218 log.debug("Processing NextObjective id{} in dev{} - add group",
219 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800220 ofdpa2GroupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800221 break;
222 case ADD_TO_EXISTING:
223 if (nextGroup != null) {
224 log.debug("Processing NextObjective id{} in dev{} - add bucket",
225 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800226 ofdpa2GroupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800227 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800228 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800229 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
230 nextObjective.id(), deviceId);
231 // by design only one pending bucket is allowed for the group
Charles Chan188ebf52015-12-23 00:15:11 -0800232 ofdpa2GroupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800233 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800234 break;
235 case REMOVE:
236 if (nextGroup == null) {
237 log.warn("Cannot remove next {} that does not exist in device {}",
238 nextObjective.id(), deviceId);
239 return;
240 }
241 log.debug("Processing NextObjective id{} in dev{} - remove group",
242 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800243 ofdpa2GroupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800244 break;
245 case REMOVE_FROM_EXISTING:
246 if (nextGroup == null) {
247 log.warn("Cannot remove from next {} that does not exist in device {}",
248 nextObjective.id(), deviceId);
249 return;
250 }
251 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
252 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800253 ofdpa2GroupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800254 break;
255 default:
Saurav Das4f980082015-11-05 13:39:15 -0800256 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700257 }
258 }
259
260 //////////////////////////////////////
261 // Flow handling
262 //////////////////////////////////////
263
264 /**
265 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
266 * and IP addresses configured on switch ports happen in different tables.
267 * Note that IP filtering rules need to be added to the ACL table, as there
268 * is no mechanism to send to controller via IP table.
269 *
270 * @param filt the filtering objective
271 * @param install indicates whether to add or remove the objective
272 * @param applicationId the application that sent this objective
273 */
274 private void processFilter(FilteringObjective filt,
275 boolean install, ApplicationId applicationId) {
276 // This driver only processes filtering criteria defined with switch
277 // ports as the key
278 PortCriterion portCriterion = null;
279 EthCriterion ethCriterion = null;
280 VlanIdCriterion vidCriterion = null;
281 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
282 if (!filt.key().equals(Criteria.dummy()) &&
283 filt.key().type() == Criterion.Type.IN_PORT) {
284 portCriterion = (PortCriterion) filt.key();
285 } else {
286 log.warn("No key defined in filtering objective from app: {}. Not"
287 + "processing filtering objective", applicationId);
288 fail(filt, ObjectiveError.UNKNOWN);
289 return;
290 }
291 // convert filtering conditions for switch-intfs into flowrules
292 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
293 for (Criterion criterion : filt.conditions()) {
294 if (criterion.type() == Criterion.Type.ETH_DST) {
295 ethCriterion = (EthCriterion) criterion;
296 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
297 vidCriterion = (VlanIdCriterion) criterion;
298 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
299 ips.add((IPCriterion) criterion);
300 } else {
301 log.error("Unsupported filter {}", criterion);
302 fail(filt, ObjectiveError.UNSUPPORTED);
303 return;
304 }
305 }
306
Saurav Das0e99e2b2015-10-28 12:39:42 -0700307 VlanId assignedVlan = null;
308 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
309 // untagged packets are assigned vlans in OF-DPA
310 if (filt.meta() == null) {
311 log.error("Missing metadata in filtering objective required "
312 + "for vlan assignment in dev {}", deviceId);
313 fail(filt, ObjectiveError.BADPARAMS);
314 return;
315 }
316 for (Instruction i : filt.meta().allInstructions()) {
317 if (i instanceof ModVlanIdInstruction) {
318 assignedVlan = ((ModVlanIdInstruction) i).vlanId();
319 }
320 }
321 if (assignedVlan == null) {
322 log.error("Driver requires an assigned vlan-id to tag incoming "
323 + "untagged packets. Not processing vlan filters on "
324 + "device {}", deviceId);
325 fail(filt, ObjectiveError.BADPARAMS);
326 return;
327 }
328 }
329
Saurav Das822c4e22015-10-23 10:51:11 -0700330 if (ethCriterion == null) {
331 log.debug("filtering objective missing dstMac, cannot program TMAC table");
332 } else {
333 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700334 vidCriterion, assignedVlan,
335 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700336 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
337 tmacRule, deviceId);
338 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
339 }
340 }
341
342 if (ethCriterion == null || vidCriterion == null) {
343 log.debug("filtering objective missing dstMac or vlan, cannot program"
344 + "Vlan Table");
345 } else {
346 for (FlowRule vlanRule : processVlanIdFilter(portCriterion, vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700347 assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700348 applicationId)) {
349 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
350 vlanRule, deviceId);
351 ops = install ? ops.add(vlanRule) : ops.remove(vlanRule);
352 }
353 }
354
355 for (IPCriterion ipaddr : ips) {
356 // since we ignore port information for IP rules, and the same (gateway) IP
357 // can be configured on multiple ports, we make sure that we send
358 // only a single rule to the switch.
359 if (!sentIpFilters.contains(ipaddr)) {
360 sentIpFilters.add(ipaddr);
361 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
362 ipaddr, deviceId);
363 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
364 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
365 selector.matchEthType(Ethernet.TYPE_IPV4);
366 selector.matchIPDst(ipaddr.ip());
367 treatment.setOutput(PortNumber.CONTROLLER);
368 FlowRule rule = DefaultFlowRule.builder()
369 .forDevice(deviceId)
370 .withSelector(selector.build())
371 .withTreatment(treatment.build())
372 .withPriority(HIGHEST_PRIORITY)
373 .fromApp(applicationId)
374 .makePermanent()
375 .forTable(ACL_TABLE).build();
376 ops = install ? ops.add(rule) : ops.remove(rule);
377 }
378 }
379
380 // apply filtering flow rules
381 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
382 @Override
383 public void onSuccess(FlowRuleOperations ops) {
384 log.info("Applied {} filtering rules in device {}",
385 ops.stages().get(0).size(), deviceId);
386 pass(filt);
387 }
388
389 @Override
390 public void onError(FlowRuleOperations ops) {
391 log.info("Failed to apply all filtering rules in dev {}", deviceId);
392 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
393 }
394 }));
395
396 }
397
398 /**
399 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700400 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700401 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700402 *
Saurav Das822c4e22015-10-23 10:51:11 -0700403 * @param portCriterion port on device for which this filter is programmed
404 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700405 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700406 * @param applicationId for application programming this filter
407 * @return list of FlowRule for port-vlan filters
408 */
409 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
410 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700411 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700412 ApplicationId applicationId) {
413 List<FlowRule> rules = new ArrayList<FlowRule>();
414 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
415 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
416 selector.matchVlanId(vidCriterion.vlanId());
Saurav Das4f980082015-11-05 13:39:15 -0800417 treatment.transition(TMAC_TABLE);
418
419 VlanId storeVlan = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700420 if (vidCriterion.vlanId() == VlanId.NONE) {
421 // untagged packets are assigned vlans
Saurav Das0e99e2b2015-10-28 12:39:42 -0700422 treatment.pushVlan().setVlanId(assignedVlan);
Saurav Das2857f382015-11-03 14:39:27 -0800423 // XXX ofdpa will require an additional vlan match on the assigned vlan
424 // and it may not require the push. This is not in compliance with OF
425 // standard. Waiting on what the exact flows are going to look like.
Saurav Das4f980082015-11-05 13:39:15 -0800426 storeVlan = assignedVlan;
427 } else {
428 storeVlan = vidCriterion.vlanId();
Saurav Das822c4e22015-10-23 10:51:11 -0700429 }
Saurav Das822c4e22015-10-23 10:51:11 -0700430
431 // ofdpa cannot match on ALL portnumber, so we need to use separate
432 // rules for each port.
433 List<PortNumber> portnums = new ArrayList<PortNumber>();
434 if (portCriterion.port() == PortNumber.ALL) {
435 for (Port port : deviceService.getPorts(deviceId)) {
436 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
437 portnums.add(port.number());
438 }
439 }
440 } else {
441 portnums.add(portCriterion.port());
442 }
Saurav Das4f980082015-11-05 13:39:15 -0800443
Saurav Das822c4e22015-10-23 10:51:11 -0700444 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800445 // update storage
Charles Chan188ebf52015-12-23 00:15:11 -0800446 ofdpa2GroupHandler.port2Vlan.put(pnum, storeVlan);
447 Set<PortNumber> vlanPorts = ofdpa2GroupHandler.vlan2Port.get(storeVlan);
Saurav Das4f980082015-11-05 13:39:15 -0800448 if (vlanPorts == null) {
449 vlanPorts = Collections.newSetFromMap(
450 new ConcurrentHashMap<PortNumber, Boolean>());
451 vlanPorts.add(pnum);
Charles Chan188ebf52015-12-23 00:15:11 -0800452 ofdpa2GroupHandler.vlan2Port.put(storeVlan, vlanPorts);
Saurav Das4f980082015-11-05 13:39:15 -0800453 } else {
454 vlanPorts.add(pnum);
455 }
456 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700457 selector.matchInPort(pnum);
458 FlowRule rule = DefaultFlowRule.builder()
459 .forDevice(deviceId)
460 .withSelector(selector.build())
461 .withTreatment(treatment.build())
462 .withPriority(DEFAULT_PRIORITY)
463 .fromApp(applicationId)
464 .makePermanent()
465 .forTable(VLAN_TABLE).build();
466 rules.add(rule);
467 }
468 return rules;
469 }
470
471 /**
472 * Allows routed packets with correct destination MAC to be directed
473 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700474 *
475 * @param portCriterion port on device for which this filter is programmed
476 * @param ethCriterion dstMac of device for which is filter is programmed
477 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700478 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700479 * @param applicationId for application programming this filter
480 * @return list of FlowRule for port-vlan filters
481
482 */
483 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
484 EthCriterion ethCriterion,
485 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700486 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700487 ApplicationId applicationId) {
488 //handling untagged packets via assigned VLAN
489 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700490 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700491 }
492 // ofdpa cannot match on ALL portnumber, so we need to use separate
493 // rules for each port.
494 List<PortNumber> portnums = new ArrayList<PortNumber>();
495 if (portCriterion.port() == PortNumber.ALL) {
496 for (Port port : deviceService.getPorts(deviceId)) {
497 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
498 portnums.add(port.number());
499 }
500 }
501 } else {
502 portnums.add(portCriterion.port());
503 }
504
505 List<FlowRule> rules = new ArrayList<FlowRule>();
506 for (PortNumber pnum : portnums) {
507 // for unicast IP packets
508 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
509 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
510 selector.matchInPort(pnum);
511 selector.matchVlanId(vidCriterion.vlanId());
512 selector.matchEthType(Ethernet.TYPE_IPV4);
513 selector.matchEthDst(ethCriterion.mac());
514 treatment.transition(UNICAST_ROUTING_TABLE);
515 FlowRule rule = DefaultFlowRule.builder()
516 .forDevice(deviceId)
517 .withSelector(selector.build())
518 .withTreatment(treatment.build())
519 .withPriority(DEFAULT_PRIORITY)
520 .fromApp(applicationId)
521 .makePermanent()
522 .forTable(TMAC_TABLE).build();
523 rules.add(rule);
524 //for MPLS packets
525 selector = DefaultTrafficSelector.builder();
526 treatment = DefaultTrafficTreatment.builder();
527 selector.matchInPort(pnum);
528 selector.matchVlanId(vidCriterion.vlanId());
529 selector.matchEthType(Ethernet.MPLS_UNICAST);
530 selector.matchEthDst(ethCriterion.mac());
531 treatment.transition(MPLS_TABLE_0);
532 rule = DefaultFlowRule.builder()
533 .forDevice(deviceId)
534 .withSelector(selector.build())
535 .withTreatment(treatment.build())
536 .withPriority(DEFAULT_PRIORITY)
537 .fromApp(applicationId)
538 .makePermanent()
539 .forTable(TMAC_TABLE).build();
540 rules.add(rule);
541 }
542 return rules;
543 }
544
545 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
546 switch (fwd.flag()) {
547 case SPECIFIC:
548 return processSpecific(fwd);
549 case VERSATILE:
550 return processVersatile(fwd);
551 default:
552 fail(fwd, ObjectiveError.UNKNOWN);
553 log.warn("Unknown forwarding flag {}", fwd.flag());
554 }
555 return Collections.emptySet();
556 }
557
558 /**
559 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
560 * ACL table.
561 * @param fwd the forwarding objective of type 'versatile'
562 * @return a collection of flow rules to be sent to the switch. An empty
563 * collection may be returned if there is a problem in processing
564 * the flow rule
565 */
566 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
567 log.info("Processing versatile forwarding objective");
568 TrafficSelector selector = fwd.selector();
569
570 EthTypeCriterion ethType =
571 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
572 if (ethType == null) {
573 log.error("Versatile forwarding objective must include ethType");
574 fail(fwd, ObjectiveError.BADPARAMS);
575 return Collections.emptySet();
576 }
577 if (fwd.nextId() == null && fwd.treatment() == null) {
578 log.error("Forwarding objective {} from {} must contain "
579 + "nextId or Treatment", fwd.selector(), fwd.appId());
580 return Collections.emptySet();
581 }
582 // XXX driver does not currently do type checking as per Tables 65-67 in
583 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
584 if (fwd.treatment() != null &&
585 fwd.treatment().allInstructions().size() == 1 &&
586 fwd.treatment().allInstructions().get(0).type() == Instruction.Type.OUTPUT) {
587 OutputInstruction o = (OutputInstruction) fwd.treatment().allInstructions().get(0);
588 if (o.port() == PortNumber.CONTROLLER) {
589 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
590 .fromApp(fwd.appId())
591 .withPriority(fwd.priority())
592 .forDevice(deviceId)
593 .withSelector(fwd.selector())
594 .withTreatment(fwd.treatment())
595 .makePermanent()
596 .forTable(ACL_TABLE);
597 return Collections.singletonList(ruleBuilder.build());
598 } else {
599 log.warn("Only allowed treatments in versatile forwarding "
600 + "objectives are punts to the controller");
601 return Collections.emptySet();
602 }
603 }
604
605 if (fwd.nextId() != null) {
606 // XXX overide case
607 log.warn("versatile objective --> next Id not yet implemeted");
608 }
609 return Collections.emptySet();
610 }
611
612 /**
613 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800614 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700615 *
616 * @param fwd the forwarding objective of type 'specific'
617 * @return a collection of flow rules. Typically there will be only one
618 * for this type of forwarding objective. An empty set may be
619 * returned if there is an issue in processing the objective.
620 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800621 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das4ce45962015-11-24 23:21:05 -0800622 log.trace("Processing specific fwd objective:{} in dev:{} with next:{}",
623 fwd.id(), deviceId, fwd.nextId());
624 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
625 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
626
627 if (isEthTypeObj) {
628 return processEthTypeSpecific(fwd);
629 } else if (isEthDstObj) {
630 return processEthDstSpecific(fwd);
631 } else {
632 log.warn("processSpecific: Unsupported forwarding objective "
633 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700634 fail(fwd, ObjectiveError.UNSUPPORTED);
635 return Collections.emptySet();
636 }
Saurav Das4ce45962015-11-24 23:21:05 -0800637 }
638
639 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
640 TrafficSelector selector = fwd.selector();
641 EthTypeCriterion ethType = (EthTypeCriterion) selector
642 .getCriterion(Criterion.Type.ETH_TYPE);
Charles Chan188ebf52015-12-23 00:15:11 -0800643 return !((ethType == null) ||
Saurav Das4ce45962015-11-24 23:21:05 -0800644 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Charles Chan188ebf52015-12-23 00:15:11 -0800645 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
Saurav Das4ce45962015-11-24 23:21:05 -0800646 }
647
648 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
649 TrafficSelector selector = fwd.selector();
650 EthCriterion ethDst = (EthCriterion) selector
651 .getCriterion(Criterion.Type.ETH_DST);
652 VlanIdCriterion vlanId = (VlanIdCriterion) selector
653 .getCriterion(Criterion.Type.VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800654 return !(ethDst == null && vlanId == null);
Saurav Das4ce45962015-11-24 23:21:05 -0800655 }
656
657 /**
658 * Handles forwarding rules to the IP and MPLS tables.
659 *
660 * @param fwd the forwarding objective
661 * @return A collection of flow rules, or an empty set
662 */
663 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
664 TrafficSelector selector = fwd.selector();
665 EthTypeCriterion ethType =
666 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700667
Charles Chan188ebf52015-12-23 00:15:11 -0800668 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800669 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
670 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
671 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
672 .matchIPDst(((IPCriterion)
673 selector.getCriterion(Criterion.Type.IPV4_DST)).ip());
674 forTableId = UNICAST_ROUTING_TABLE;
Saurav Das4ce45962015-11-24 23:21:05 -0800675 log.debug("processing IPv4 specific forwarding objective {} -> next:{}"
676 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800677 } else {
678 filteredSelector
679 .matchEthType(Ethernet.MPLS_UNICAST)
680 .matchMplsLabel(((MplsCriterion)
681 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
682 MplsBosCriterion bos = (MplsBosCriterion) selector
683 .getCriterion(Criterion.Type.MPLS_BOS);
684 if (bos != null) {
685 filteredSelector.matchMplsBos(bos.mplsBos());
686 }
687 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800688 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
689 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800690 }
Saurav Das822c4e22015-10-23 10:51:11 -0700691
692 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Saurav Das8a0732e2015-11-20 15:27:53 -0800693 boolean popMpls = false;
694 if (fwd.treatment() != null) {
695 for (Instruction i : fwd.treatment().allInstructions()) {
Charles Chan7d10b162015-12-07 18:54:45 -0800696 /*
697 * NOTE: OF-DPA does not support immediate instruction in
698 * L3 unicast and MPLS table.
699 */
700 tb.deferred().add(i);
Saurav Das8a0732e2015-11-20 15:27:53 -0800701 if (i instanceof L2ModificationInstruction &&
702 ((L2ModificationInstruction) i).subtype() == L2SubType.MPLS_POP) {
703 popMpls = true;
704 }
705 }
706 }
Saurav Das822c4e22015-10-23 10:51:11 -0700707
708 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800709 if (forTableId == MPLS_TABLE_1 && !popMpls) {
710 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
711 + "is not implemented in OF-DPA yet. Aborting this flow "
712 + "in this device {}", deviceId);
713 // XXX We could convert to forwarding to a single-port, via a
714 // MPLS interface, or a MPLS SWAP (with-same) but that would
715 // have to be handled in the next-objective. Also the pop-mpls
716 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800717 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800718 return Collections.emptySet();
719 }
720
Saurav Das423fe2b2015-12-04 10:52:59 -0800721 NextGroup next = getGroupForNextObjective(fwd.nextId());
722 if (next != null) {
723 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
724 // we only need the top level group's key to point the flow to it
725 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
726 if (group == null) {
727 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
728 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
729 fail(fwd, ObjectiveError.GROUPMISSING);
730 return Collections.emptySet();
731 }
732 tb.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700733 }
Saurav Das822c4e22015-10-23 10:51:11 -0700734 }
735 tb.transition(ACL_TABLE);
736 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
737 .fromApp(fwd.appId())
738 .withPriority(fwd.priority())
739 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800740 .withSelector(filteredSelector.build())
741 .withTreatment(tb.build())
742 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700743
744 if (fwd.permanent()) {
745 ruleBuilder.makePermanent();
746 } else {
747 ruleBuilder.makeTemporary(fwd.timeout());
748 }
749
Saurav Das822c4e22015-10-23 10:51:11 -0700750 return Collections.singletonList(ruleBuilder.build());
751 }
752
Saurav Das4ce45962015-11-24 23:21:05 -0800753 /**
754 * Handles forwarding rules to the L2 bridging table. Flow actions are not
755 * allowed in the bridging table - instead we use L2 Interface group or
756 * L2 flood group
757 *
758 * @param fwd the forwarding objective
759 * @return A collection of flow rules, or an empty set
760 */
761 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
762 List<FlowRule> rules = new ArrayList<>();
763
764 // Build filtered selector
765 TrafficSelector selector = fwd.selector();
766 EthCriterion ethCriterion = (EthCriterion) selector
767 .getCriterion(Criterion.Type.ETH_DST);
768 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
769 .getCriterion(Criterion.Type.VLAN_VID);
770
771 if (vlanIdCriterion == null) {
772 log.warn("Forwarding objective for bridging requires vlan. Not "
773 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
774 fail(fwd, ObjectiveError.BADPARAMS);
775 return Collections.emptySet();
776 }
777
778 TrafficSelector.Builder filteredSelectorBuilder =
779 DefaultTrafficSelector.builder();
780 // Do not match MacAddress for subnet broadcast entry
781 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
782 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
783 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
784 fwd.id(), fwd.nextId(), deviceId);
785 } else {
786 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
787 + "in dev:{} for vlan:{}",
788 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
789 }
790 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
791 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
792
793 if (fwd.treatment() != null) {
794 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
795 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
796 }
797
798 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
799 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800800 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -0800801 if (next != null) {
802 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
803 // we only need the top level group's key to point the flow to it
804 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
805 if (group != null) {
806 treatmentBuilder.deferred().group(group.id());
807 } else {
808 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
809 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
810 fail(fwd, ObjectiveError.GROUPMISSING);
811 return Collections.emptySet();
812 }
813 }
814 }
815 treatmentBuilder.immediate().transition(ACL_TABLE);
816 TrafficTreatment filteredTreatment = treatmentBuilder.build();
817
818 // Build bridging table entries
819 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
820 flowRuleBuilder.fromApp(fwd.appId())
821 .withPriority(fwd.priority())
822 .forDevice(deviceId)
823 .withSelector(filteredSelector)
824 .withTreatment(filteredTreatment)
825 .forTable(BRIDGING_TABLE);
826 if (fwd.permanent()) {
827 flowRuleBuilder.makePermanent();
828 } else {
829 flowRuleBuilder.makeTemporary(fwd.timeout());
830 }
831 rules.add(flowRuleBuilder.build());
832 return rules;
833 }
834
Saurav Das423fe2b2015-12-04 10:52:59 -0800835 protected NextGroup getGroupForNextObjective(Integer nextId) {
836 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
837 if (next != null) {
838 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
839 if (gkeys != null && !gkeys.isEmpty()) {
840 return next;
841 } else {
842 log.warn("Empty next group found in FlowObjective store for "
843 + "next-id:{} in dev:{}", nextId, deviceId);
844 }
845 } else {
846 log.warn("next-id {} not found in Flow objective store for dev:{}",
847 nextId, deviceId);
848 }
849 return null;
850 }
851
Charles Chan188ebf52015-12-23 00:15:11 -0800852 protected static void pass(Objective obj) {
Saurav Das822c4e22015-10-23 10:51:11 -0700853 if (obj.context().isPresent()) {
854 obj.context().get().onSuccess(obj);
855 }
856 }
857
Charles Chan188ebf52015-12-23 00:15:11 -0800858 protected static void fail(Objective obj, ObjectiveError error) {
Saurav Das822c4e22015-10-23 10:51:11 -0700859 if (obj.context().isPresent()) {
860 obj.context().get().onError(obj, error);
861 }
862 }
Saurav Das822c4e22015-10-23 10:51:11 -0700863}