blob: e4b7ef529764bb5f0747e368f5876a621989602b [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;
Charles Chane849c192016-01-11 18:28:54 -0800308 // For VLAN cross-connect packets, use the configured VLAN
309 if (vidCriterion != null) {
310 if (vidCriterion.vlanId() != VlanId.NONE) {
311 assignedVlan = vidCriterion.vlanId();
312
313 // For untagged packets, assign a VLAN ID
314 } else {
315 if (filt.meta() == null) {
316 log.error("Missing metadata in filtering objective required " +
317 "for vlan assignment in dev {}", deviceId);
318 fail(filt, ObjectiveError.BADPARAMS);
319 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700320 }
Charles Chane849c192016-01-11 18:28:54 -0800321 for (Instruction i : filt.meta().allInstructions()) {
322 if (i instanceof ModVlanIdInstruction) {
323 assignedVlan = ((ModVlanIdInstruction) i).vlanId();
324 }
325 }
326 if (assignedVlan == null) {
327 log.error("Driver requires an assigned vlan-id to tag incoming "
328 + "untagged packets. Not processing vlan filters on "
329 + "device {}", deviceId);
330 fail(filt, ObjectiveError.BADPARAMS);
331 return;
332 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700333 }
334 }
335
Charles Chane849c192016-01-11 18:28:54 -0800336 if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700337 log.debug("filtering objective missing dstMac, cannot program TMAC table");
338 } else {
339 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700340 vidCriterion, assignedVlan,
341 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700342 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
343 tmacRule, deviceId);
344 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
345 }
346 }
347
348 if (ethCriterion == null || vidCriterion == null) {
Charles Chane849c192016-01-11 18:28:54 -0800349 log.debug("filtering objective missing dstMac or VLAN, "
350 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700351 } else {
352 for (FlowRule vlanRule : processVlanIdFilter(portCriterion, vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700353 assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700354 applicationId)) {
355 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
356 vlanRule, deviceId);
357 ops = install ? ops.add(vlanRule) : ops.remove(vlanRule);
358 }
359 }
360
361 for (IPCriterion ipaddr : ips) {
362 // since we ignore port information for IP rules, and the same (gateway) IP
363 // can be configured on multiple ports, we make sure that we send
364 // only a single rule to the switch.
365 if (!sentIpFilters.contains(ipaddr)) {
366 sentIpFilters.add(ipaddr);
367 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
368 ipaddr, deviceId);
369 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
370 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
371 selector.matchEthType(Ethernet.TYPE_IPV4);
372 selector.matchIPDst(ipaddr.ip());
373 treatment.setOutput(PortNumber.CONTROLLER);
374 FlowRule rule = DefaultFlowRule.builder()
375 .forDevice(deviceId)
376 .withSelector(selector.build())
377 .withTreatment(treatment.build())
378 .withPriority(HIGHEST_PRIORITY)
379 .fromApp(applicationId)
380 .makePermanent()
381 .forTable(ACL_TABLE).build();
382 ops = install ? ops.add(rule) : ops.remove(rule);
383 }
384 }
385
386 // apply filtering flow rules
387 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
388 @Override
389 public void onSuccess(FlowRuleOperations ops) {
390 log.info("Applied {} filtering rules in device {}",
391 ops.stages().get(0).size(), deviceId);
392 pass(filt);
393 }
394
395 @Override
396 public void onError(FlowRuleOperations ops) {
397 log.info("Failed to apply all filtering rules in dev {}", deviceId);
398 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
399 }
400 }));
401
402 }
403
404 /**
405 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700406 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700407 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700408 *
Saurav Das822c4e22015-10-23 10:51:11 -0700409 * @param portCriterion port on device for which this filter is programmed
410 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700411 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700412 * @param applicationId for application programming this filter
413 * @return list of FlowRule for port-vlan filters
414 */
415 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
416 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700417 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700418 ApplicationId applicationId) {
419 List<FlowRule> rules = new ArrayList<FlowRule>();
420 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
421 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
422 selector.matchVlanId(vidCriterion.vlanId());
Saurav Das4f980082015-11-05 13:39:15 -0800423 treatment.transition(TMAC_TABLE);
424
425 VlanId storeVlan = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700426 if (vidCriterion.vlanId() == VlanId.NONE) {
427 // untagged packets are assigned vlans
Saurav Das0e99e2b2015-10-28 12:39:42 -0700428 treatment.pushVlan().setVlanId(assignedVlan);
Saurav Das2857f382015-11-03 14:39:27 -0800429 // XXX ofdpa will require an additional vlan match on the assigned vlan
430 // and it may not require the push. This is not in compliance with OF
431 // standard. Waiting on what the exact flows are going to look like.
Saurav Das4f980082015-11-05 13:39:15 -0800432 storeVlan = assignedVlan;
433 } else {
434 storeVlan = vidCriterion.vlanId();
Saurav Das822c4e22015-10-23 10:51:11 -0700435 }
Saurav Das822c4e22015-10-23 10:51:11 -0700436
437 // ofdpa cannot match on ALL portnumber, so we need to use separate
438 // rules for each port.
439 List<PortNumber> portnums = new ArrayList<PortNumber>();
440 if (portCriterion.port() == PortNumber.ALL) {
441 for (Port port : deviceService.getPorts(deviceId)) {
442 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
443 portnums.add(port.number());
444 }
445 }
446 } else {
447 portnums.add(portCriterion.port());
448 }
Saurav Das4f980082015-11-05 13:39:15 -0800449
Saurav Das822c4e22015-10-23 10:51:11 -0700450 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800451 // update storage
Charles Chan188ebf52015-12-23 00:15:11 -0800452 ofdpa2GroupHandler.port2Vlan.put(pnum, storeVlan);
453 Set<PortNumber> vlanPorts = ofdpa2GroupHandler.vlan2Port.get(storeVlan);
Saurav Das4f980082015-11-05 13:39:15 -0800454 if (vlanPorts == null) {
455 vlanPorts = Collections.newSetFromMap(
456 new ConcurrentHashMap<PortNumber, Boolean>());
457 vlanPorts.add(pnum);
Charles Chan188ebf52015-12-23 00:15:11 -0800458 ofdpa2GroupHandler.vlan2Port.put(storeVlan, vlanPorts);
Saurav Das4f980082015-11-05 13:39:15 -0800459 } else {
460 vlanPorts.add(pnum);
461 }
462 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700463 selector.matchInPort(pnum);
464 FlowRule rule = DefaultFlowRule.builder()
465 .forDevice(deviceId)
466 .withSelector(selector.build())
467 .withTreatment(treatment.build())
468 .withPriority(DEFAULT_PRIORITY)
469 .fromApp(applicationId)
470 .makePermanent()
471 .forTable(VLAN_TABLE).build();
472 rules.add(rule);
473 }
474 return rules;
475 }
476
477 /**
478 * Allows routed packets with correct destination MAC to be directed
479 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700480 *
481 * @param portCriterion port on device for which this filter is programmed
482 * @param ethCriterion dstMac of device for which is filter is programmed
483 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700484 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700485 * @param applicationId for application programming this filter
486 * @return list of FlowRule for port-vlan filters
487
488 */
489 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
490 EthCriterion ethCriterion,
491 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700492 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700493 ApplicationId applicationId) {
494 //handling untagged packets via assigned VLAN
495 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700496 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700497 }
498 // ofdpa cannot match on ALL portnumber, so we need to use separate
499 // rules for each port.
500 List<PortNumber> portnums = new ArrayList<PortNumber>();
501 if (portCriterion.port() == PortNumber.ALL) {
502 for (Port port : deviceService.getPorts(deviceId)) {
503 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
504 portnums.add(port.number());
505 }
506 }
507 } else {
508 portnums.add(portCriterion.port());
509 }
510
511 List<FlowRule> rules = new ArrayList<FlowRule>();
512 for (PortNumber pnum : portnums) {
513 // for unicast IP packets
514 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
515 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
516 selector.matchInPort(pnum);
517 selector.matchVlanId(vidCriterion.vlanId());
518 selector.matchEthType(Ethernet.TYPE_IPV4);
519 selector.matchEthDst(ethCriterion.mac());
520 treatment.transition(UNICAST_ROUTING_TABLE);
521 FlowRule rule = DefaultFlowRule.builder()
522 .forDevice(deviceId)
523 .withSelector(selector.build())
524 .withTreatment(treatment.build())
525 .withPriority(DEFAULT_PRIORITY)
526 .fromApp(applicationId)
527 .makePermanent()
528 .forTable(TMAC_TABLE).build();
529 rules.add(rule);
530 //for MPLS packets
531 selector = DefaultTrafficSelector.builder();
532 treatment = DefaultTrafficTreatment.builder();
533 selector.matchInPort(pnum);
534 selector.matchVlanId(vidCriterion.vlanId());
535 selector.matchEthType(Ethernet.MPLS_UNICAST);
536 selector.matchEthDst(ethCriterion.mac());
537 treatment.transition(MPLS_TABLE_0);
538 rule = DefaultFlowRule.builder()
539 .forDevice(deviceId)
540 .withSelector(selector.build())
541 .withTreatment(treatment.build())
542 .withPriority(DEFAULT_PRIORITY)
543 .fromApp(applicationId)
544 .makePermanent()
545 .forTable(TMAC_TABLE).build();
546 rules.add(rule);
547 }
548 return rules;
549 }
550
551 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
552 switch (fwd.flag()) {
553 case SPECIFIC:
554 return processSpecific(fwd);
555 case VERSATILE:
556 return processVersatile(fwd);
557 default:
558 fail(fwd, ObjectiveError.UNKNOWN);
559 log.warn("Unknown forwarding flag {}", fwd.flag());
560 }
561 return Collections.emptySet();
562 }
563
564 /**
565 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
566 * ACL table.
567 * @param fwd the forwarding objective of type 'versatile'
568 * @return a collection of flow rules to be sent to the switch. An empty
569 * collection may be returned if there is a problem in processing
570 * the flow rule
571 */
572 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
573 log.info("Processing versatile forwarding objective");
574 TrafficSelector selector = fwd.selector();
575
576 EthTypeCriterion ethType =
577 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
578 if (ethType == null) {
579 log.error("Versatile forwarding objective must include ethType");
580 fail(fwd, ObjectiveError.BADPARAMS);
581 return Collections.emptySet();
582 }
583 if (fwd.nextId() == null && fwd.treatment() == null) {
584 log.error("Forwarding objective {} from {} must contain "
585 + "nextId or Treatment", fwd.selector(), fwd.appId());
586 return Collections.emptySet();
587 }
588 // XXX driver does not currently do type checking as per Tables 65-67 in
589 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
590 if (fwd.treatment() != null &&
591 fwd.treatment().allInstructions().size() == 1 &&
592 fwd.treatment().allInstructions().get(0).type() == Instruction.Type.OUTPUT) {
593 OutputInstruction o = (OutputInstruction) fwd.treatment().allInstructions().get(0);
594 if (o.port() == PortNumber.CONTROLLER) {
595 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
596 .fromApp(fwd.appId())
597 .withPriority(fwd.priority())
598 .forDevice(deviceId)
599 .withSelector(fwd.selector())
600 .withTreatment(fwd.treatment())
601 .makePermanent()
602 .forTable(ACL_TABLE);
603 return Collections.singletonList(ruleBuilder.build());
604 } else {
605 log.warn("Only allowed treatments in versatile forwarding "
606 + "objectives are punts to the controller");
607 return Collections.emptySet();
608 }
609 }
610
611 if (fwd.nextId() != null) {
612 // XXX overide case
613 log.warn("versatile objective --> next Id not yet implemeted");
614 }
615 return Collections.emptySet();
616 }
617
618 /**
619 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800620 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700621 *
622 * @param fwd the forwarding objective of type 'specific'
623 * @return a collection of flow rules. Typically there will be only one
624 * for this type of forwarding objective. An empty set may be
625 * returned if there is an issue in processing the objective.
626 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800627 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das4ce45962015-11-24 23:21:05 -0800628 log.trace("Processing specific fwd objective:{} in dev:{} with next:{}",
629 fwd.id(), deviceId, fwd.nextId());
630 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
631 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
632
633 if (isEthTypeObj) {
634 return processEthTypeSpecific(fwd);
635 } else if (isEthDstObj) {
636 return processEthDstSpecific(fwd);
637 } else {
638 log.warn("processSpecific: Unsupported forwarding objective "
639 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700640 fail(fwd, ObjectiveError.UNSUPPORTED);
641 return Collections.emptySet();
642 }
Saurav Das4ce45962015-11-24 23:21:05 -0800643 }
644
645 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
646 TrafficSelector selector = fwd.selector();
647 EthTypeCriterion ethType = (EthTypeCriterion) selector
648 .getCriterion(Criterion.Type.ETH_TYPE);
Charles Chan188ebf52015-12-23 00:15:11 -0800649 return !((ethType == null) ||
Saurav Das4ce45962015-11-24 23:21:05 -0800650 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Charles Chan188ebf52015-12-23 00:15:11 -0800651 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
Saurav Das4ce45962015-11-24 23:21:05 -0800652 }
653
654 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
655 TrafficSelector selector = fwd.selector();
656 EthCriterion ethDst = (EthCriterion) selector
657 .getCriterion(Criterion.Type.ETH_DST);
658 VlanIdCriterion vlanId = (VlanIdCriterion) selector
659 .getCriterion(Criterion.Type.VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800660 return !(ethDst == null && vlanId == null);
Saurav Das4ce45962015-11-24 23:21:05 -0800661 }
662
663 /**
664 * Handles forwarding rules to the IP and MPLS tables.
665 *
666 * @param fwd the forwarding objective
667 * @return A collection of flow rules, or an empty set
668 */
669 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
670 TrafficSelector selector = fwd.selector();
671 EthTypeCriterion ethType =
672 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700673
Charles Chan188ebf52015-12-23 00:15:11 -0800674 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800675 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
676 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
677 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
678 .matchIPDst(((IPCriterion)
679 selector.getCriterion(Criterion.Type.IPV4_DST)).ip());
680 forTableId = UNICAST_ROUTING_TABLE;
Saurav Das4ce45962015-11-24 23:21:05 -0800681 log.debug("processing IPv4 specific forwarding objective {} -> next:{}"
682 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800683 } else {
684 filteredSelector
685 .matchEthType(Ethernet.MPLS_UNICAST)
686 .matchMplsLabel(((MplsCriterion)
687 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
688 MplsBosCriterion bos = (MplsBosCriterion) selector
689 .getCriterion(Criterion.Type.MPLS_BOS);
690 if (bos != null) {
691 filteredSelector.matchMplsBos(bos.mplsBos());
692 }
693 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800694 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
695 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800696 }
Saurav Das822c4e22015-10-23 10:51:11 -0700697
698 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Saurav Das8a0732e2015-11-20 15:27:53 -0800699 boolean popMpls = false;
700 if (fwd.treatment() != null) {
701 for (Instruction i : fwd.treatment().allInstructions()) {
Charles Chan7d10b162015-12-07 18:54:45 -0800702 /*
703 * NOTE: OF-DPA does not support immediate instruction in
704 * L3 unicast and MPLS table.
705 */
706 tb.deferred().add(i);
Saurav Das8a0732e2015-11-20 15:27:53 -0800707 if (i instanceof L2ModificationInstruction &&
708 ((L2ModificationInstruction) i).subtype() == L2SubType.MPLS_POP) {
709 popMpls = true;
710 }
711 }
712 }
Saurav Das822c4e22015-10-23 10:51:11 -0700713
714 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800715 if (forTableId == MPLS_TABLE_1 && !popMpls) {
716 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
717 + "is not implemented in OF-DPA yet. Aborting this flow "
718 + "in this device {}", deviceId);
719 // XXX We could convert to forwarding to a single-port, via a
720 // MPLS interface, or a MPLS SWAP (with-same) but that would
721 // have to be handled in the next-objective. Also the pop-mpls
722 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800723 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800724 return Collections.emptySet();
725 }
726
Saurav Das423fe2b2015-12-04 10:52:59 -0800727 NextGroup next = getGroupForNextObjective(fwd.nextId());
728 if (next != null) {
729 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
730 // we only need the top level group's key to point the flow to it
731 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
732 if (group == null) {
733 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
734 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
735 fail(fwd, ObjectiveError.GROUPMISSING);
736 return Collections.emptySet();
737 }
738 tb.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700739 }
Saurav Das822c4e22015-10-23 10:51:11 -0700740 }
741 tb.transition(ACL_TABLE);
742 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
743 .fromApp(fwd.appId())
744 .withPriority(fwd.priority())
745 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800746 .withSelector(filteredSelector.build())
747 .withTreatment(tb.build())
748 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700749
750 if (fwd.permanent()) {
751 ruleBuilder.makePermanent();
752 } else {
753 ruleBuilder.makeTemporary(fwd.timeout());
754 }
755
Saurav Das822c4e22015-10-23 10:51:11 -0700756 return Collections.singletonList(ruleBuilder.build());
757 }
758
Saurav Das4ce45962015-11-24 23:21:05 -0800759 /**
760 * Handles forwarding rules to the L2 bridging table. Flow actions are not
761 * allowed in the bridging table - instead we use L2 Interface group or
762 * L2 flood group
763 *
764 * @param fwd the forwarding objective
765 * @return A collection of flow rules, or an empty set
766 */
767 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
768 List<FlowRule> rules = new ArrayList<>();
769
770 // Build filtered selector
771 TrafficSelector selector = fwd.selector();
772 EthCriterion ethCriterion = (EthCriterion) selector
773 .getCriterion(Criterion.Type.ETH_DST);
774 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
775 .getCriterion(Criterion.Type.VLAN_VID);
776
777 if (vlanIdCriterion == null) {
778 log.warn("Forwarding objective for bridging requires vlan. Not "
779 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
780 fail(fwd, ObjectiveError.BADPARAMS);
781 return Collections.emptySet();
782 }
783
784 TrafficSelector.Builder filteredSelectorBuilder =
785 DefaultTrafficSelector.builder();
786 // Do not match MacAddress for subnet broadcast entry
787 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
788 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
789 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
790 fwd.id(), fwd.nextId(), deviceId);
791 } else {
792 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
793 + "in dev:{} for vlan:{}",
794 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
795 }
796 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
797 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
798
799 if (fwd.treatment() != null) {
800 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
801 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
802 }
803
804 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
805 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800806 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -0800807 if (next != null) {
808 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
809 // we only need the top level group's key to point the flow to it
810 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
811 if (group != null) {
812 treatmentBuilder.deferred().group(group.id());
813 } else {
814 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
815 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
816 fail(fwd, ObjectiveError.GROUPMISSING);
817 return Collections.emptySet();
818 }
819 }
820 }
821 treatmentBuilder.immediate().transition(ACL_TABLE);
822 TrafficTreatment filteredTreatment = treatmentBuilder.build();
823
824 // Build bridging table entries
825 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
826 flowRuleBuilder.fromApp(fwd.appId())
827 .withPriority(fwd.priority())
828 .forDevice(deviceId)
829 .withSelector(filteredSelector)
830 .withTreatment(filteredTreatment)
831 .forTable(BRIDGING_TABLE);
832 if (fwd.permanent()) {
833 flowRuleBuilder.makePermanent();
834 } else {
835 flowRuleBuilder.makeTemporary(fwd.timeout());
836 }
837 rules.add(flowRuleBuilder.build());
838 return rules;
839 }
840
Saurav Das423fe2b2015-12-04 10:52:59 -0800841 protected NextGroup getGroupForNextObjective(Integer nextId) {
842 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
843 if (next != null) {
844 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
845 if (gkeys != null && !gkeys.isEmpty()) {
846 return next;
847 } else {
848 log.warn("Empty next group found in FlowObjective store for "
849 + "next-id:{} in dev:{}", nextId, deviceId);
850 }
851 } else {
852 log.warn("next-id {} not found in Flow objective store for dev:{}",
853 nextId, deviceId);
854 }
855 return null;
856 }
857
Charles Chan188ebf52015-12-23 00:15:11 -0800858 protected static void pass(Objective obj) {
Saurav Das822c4e22015-10-23 10:51:11 -0700859 if (obj.context().isPresent()) {
860 obj.context().get().onSuccess(obj);
861 }
862 }
863
Charles Chan188ebf52015-12-23 00:15:11 -0800864 protected static void fail(Objective obj, ObjectiveError error) {
Saurav Das822c4e22015-10-23 10:51:11 -0700865 if (obj.context().isPresent()) {
866 obj.context().get().onError(obj, error);
867 }
868 }
Saurav Das822c4e22015-10-23 10:51:11 -0700869}